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, it may be similar to
Copy the code code as follows:
class Integer{
final int value; //Once assigned, it cannot be changed.
}
This shows up: the address value passed when calling cannot change the outer layer + the object itself cannot be changed. 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 dedicated AtomicInteger atomic integer object
Copy the code code as follows:
public static void main(String[] parameter) {
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 option 1, try to avoid