Is it a good practice to change arguments in Java

前端 未结 6 797
小蘑菇
小蘑菇 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条回答
  •  猫巷女王i
    2020-12-16 18:33

    It's merely a personal opinion, but I think it can be confusing for other people that may want to use the original parameter value later in that code, and may not notice that it has already been changed.

    Plus, it's cheap to simply create another variable and assign it the modified value (i.e., int j = i + 1).

提交回复
热议问题