简体   繁体   中英

Why isn't the value updated?

It is a very basic question, but I don't seem to understand why this doesn't work. As far as I know, a and b would be pointers (in C thinking) to Integer objects. Why is the output 3 2 and not 3 3 ? I would have expected the value of b to also be incremented when incrementing a.

Integer a = new Integer(1);
Integer b = new Integer(2);
a = b;
a++;
System.out.print(a + " " + b);

The keyword here is auto-boxing

https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Basically, the java compiler automatically converts between the Integer class and the primitive type int in the "appropriate" context. One such context is assignment.

Integer is immutable, and a++ , like a = a + 1 , sets a to refer to a different immutable object.

In other words: a = b sets a to refer to the same object as b , but after a++ , a and b refer to different objects again.

It is not equivalent to the C code that I suspect you're thinking of;

int *a = malloc(sizeof(*a));
*a = 1;
int *b = malloc(sizeof(*b));
*b = 2;
a = b;
(*a)++;

Thinking about Java references in terms of C pointers, or C++ references, easily leads the mind astray.

Firstly, in case of java, the term used is an object 'reference' and not a 'pointer'. Basically it means that its a logical reference to the actual object.

Further more as already noted by Lagerbaer, its autoboxing-unboxing that is transparent which effectively increments the value, creates a new object and then assigns it back to the reference.

So at the end of the increment operation, there are two objects instead of one.

The increment operation after unboxing would probably look something like this :

a = Integer.valueOf(a.intValue()++);

Integer是一个不可变类型,因此您无法更改方法中的值。

a = Integer.valueOf(a.intValue() + 1);

Before the a++ instruction you have:

      +---------+
a --->| Integer |
b --->|    2    |
      +---------+

Both a and b pointing at the same Integer object with value 2.

With the a++ instruction, Java autoboxing does these steps automatically for you:

  1. Convert the Integer with value 2 to a primitive type int with value 2.
  2. Increment the primitive value int to 3.
  3. Convert the primitive type int with value 3 to an Integer with value 3, which will give you a new instance. As other pointed out, Integer is an immutable class, so you get different objects for different values.

So you end up with:

      +---------+         +---------+
a --->| Integer |   b --->| Integer |
      |    2    |         |    3    |
      +---------+         +---------+

你正在递增a的值,这对a的值没有任何影响,因为ba无关。

As Sean already answered, Integer (just like String) is immutable, that means it's value can't be changed. So your call to a++ actually created a new Integer with value a+1 and stored that as a . Meanwhile b is still pointing at the old Integer.

This can be shown by comparing the References ( == operator). Before a++ a==b returns true (same reference/object). After a++ a==b returns false , as a is now pointing at a new Integer object.

Why do you think b should be 3? You never change that value!

Integer a = new Integer(1);     // a is 1
Integer b = new Integer(2);     // b is 2
a = b;                          // a now is 2
a++;                            // a now is 3
System.out.print(a + " " + b);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM