Why is the update not updating?

This is a very simple question, but I don't seem to understand why this is not working. As far as I know, a and b will be pointers (in C-thinking) to Integer objects. Why is the conclusion 3 2 , not 3 3 ? I would expect the value of b to increase as well when adding a.

 Integer a = new Integer(1); Integer b = new Integer(2); a = b; a++; System.out.print(a + " " + b); 
+6
source share
8 answers

First, in the case of java, the term used is a reference to an object, not a "pointer". This basically means that its a logical reference to the actual object.

Further, as Lagerbaer already noted, its autoboxing-unboxing , which is transparent, which effectively increases the value, creates a new object, and then redirects it back to the link.

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

The increment operation after unpacking will probably look something like this:

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

The key word here is auto-boxing.

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

Basically, the java compiler automatically converts the Integer class and primitive int type to an β€œappropriate” context. One such context is assignment.

+4
source

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

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

This is not equivalent to the C code that I suspect

 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 is easily misleading.

+4
source

The integer is immutable, so you cannot change the value in a method.

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

Before the a++ command, you have:

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

Both a and b point to the same Integer object with a value of 2.

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

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

So you are done:

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

You increase the value of a, which will not affect the value of be, since b not related to a .

+1
source

As Sean already said, Integer (like String) is immutable, which means that the value cannot be changed. So your call to a++ actually created a new Integer with the value a + 1 and saved it as a . Meanwhile, b still points to the old Integer.

This can be shown by comparing the Links operator ( == ). Prior to a++ a==b returns true (same object / object). After a++ a==b returns false , since a now points to a new Integer object.

+1
source

Why do you think b should be 3? You never change this 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); 
0
source

Source: https://habr.com/ru/post/1014240/


All Articles