Is it a good practice to change arguments in Java

前端 未结 6 792
小蘑菇
小蘑菇 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:53

    What is important to note is that i = i + 1; does not really change i. It only changes your local copy of i (in other words, the i in the calling code won't change).

    Based on that, it is a matter of readability and avoiding unexpected behaviour in your code by complying with the POLS (Principle Of Least Surprise).

提交回复
热议问题