Reputation: 2262
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);
Upvotes: 6
Views: 431
Reputation: 66371
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.
Upvotes: 4
Reputation: 10948
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:
Integer
with value 2 to a primitive type int
with value 2.int
to 3.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 |
+---------+ +---------+
Upvotes: 2
Reputation: 2608
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()++);
Upvotes: 4
Reputation: 5599
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);
Upvotes: 0
Reputation: 630
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.
Upvotes: 1
Reputation: 73
You are incrementing the value of a which will does not have any affect on the value of be as b
is not related to a
.
Upvotes: 1
Reputation: 9617
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.
Upvotes: 4
Reputation: 411
Integer is an immutable type, therefore you can't change the value within the method.
a = Integer.valueOf(a.intValue() + 1);
Upvotes: 2