Toma Radu-Petrescu
Toma Radu-Petrescu

Reputation: 2262

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);

Upvotes: 6

Views: 431

Answers (8)

molbdnilo
molbdnilo

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

ericbn
ericbn

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:

  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    |
      +---------+         +---------+

Upvotes: 2

Ravindra HV
Ravindra HV

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

Andreas
Andreas

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

Michael Ritter
Michael Ritter

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

Suyash Mittal
Suyash Mittal

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

cadolphs
cadolphs

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

Sean Kennedy
Sean Kennedy

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

Related Questions