Is it a good practice to change arguments in Java

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

    Since i is passed by value it is safe to change it foo()

    It is absolutely safe even when you pass an object reference because they are local: i.e., assigning a new reference to the local reference will not have any impact on the original reference in the calling code

    It's your personal choice. However i would not change the argument values as one might loose the track of the actual value passed in to this method.

提交回复
热议问题