Java itself is a value-passing call, and the address value is passed to the object. Reassigning the address value is equivalent to repointing and will not affect the outer layer.
And the Integer object here also has special characteristics. In fact, the implementation may be similar to
class Integer{ final int value; //一旦赋值,就不能改变。 }
This shows up: the address value passed when calling cannot change the outer + object itself and cannot change. As a result, this value cannot be changed
There are many solutions
1. The Java style is to use the return value for a single value. return i; assign i=foo(); outside; use arrays or objects for multiple values.
2. Pass your own encapsulation class. class MutableInteger{ int value;}
3. Pass the special AtomicInteger atomic integer object
public static void main(String[] 参数) { AtomicInteger i=new AtomicInteger(40); i.intValue(); System.out.println(i); } public static void change(AtomicInteger i) { i.set(55); }
You can also change the value after passing it.
Recommended solution 1, try to avoid
Change Please pay attention to the PHP Chinese website for more related articles on the problem of Integer parameter passing in Java!