Is it a good practice to change arguments in Java

前端 未结 6 789
小蘑菇
小蘑菇 2020-12-16 17:54

Suppose I am writing a method foo(int i) in Java.
Since i is passed by value it is safe to change it in foo. For example



        
6条回答
  •  温柔的废话
    2020-12-16 18:58

    Neutral. But it would be considered a better practice by many people to change the method to:

    void foo(final int i) {
        int j = i + 1; // not change i
        ...
    }
    

    Feel free to work either way.

提交回复
热议问题