0

When we assign objects of the same type to one another, the new object stores the address of the object that's being assigned to it. For instance, if I have my own class named CLA then the following code will produce 11 as the output:

public class CLA{
    int val;
    public static void main(String...args){
        CLA ob1 = new CLA();
        ob1.val = 10;
        CLA ob2 = ob1;
        ob1.val++;
        System.out.printf("%d", ob2.val);
    }
}

because ob2 would refer to ob1 and consequently show ob1's val.

However, the same doesn't happen for Wrapper classes. When I assign an Integer object to another, the operation simply behaves like we're dealing with values and not objects. As opposed to the above code, the output of the below code is 10:

Integer x = 10; //auto-boxed as new Integer(10)
Integer y = x;
x++;
System.out.printf("%d", y.intValue());


Why does this happen?

Do wrapper class objects get unboxed while being assigned to another object, so as to pass the value instead of the address??

progyammer
  • 1,498
  • 3
  • 17
  • 29
  • The Java primitive wrapper types are immutable. – Elliott Frisch Sep 30 '17 at 18:39
  • @ElliottFrisch But if I display **x**'s value, the output is `11`. – progyammer Sep 30 '17 at 18:40
  • 4
    `x++` is equivalent to `x = Integer.valueOf(x.intValue() + 1)`. It doesn't change the state of the Integer referenced by x. It assigns another Integer to x. Wrapper classes are all immutable: their state can't change. – JB Nizet Sep 30 '17 at 18:43
  • @JBNizet _"It does't change the state of the integer referenced by x; it assigns another integer to x."_ I didn't get it: doesn't it mean that there are two values on x??? – progyammer Sep 30 '17 at 18:45
  • 2
    x was a reference to a given Integer object, containing the value 10. And after `x++` is executed, x references a different Integer object, containing the value 11. I don't know how I can explain it differently. And I don't know what you mean by "there are two values on x". – JB Nizet Sep 30 '17 at 18:50
  • It's clear to me now. I was a little confused earlier as I had hovered over the _"immutable"_ part. – progyammer Sep 30 '17 at 18:50

2 Answers2

1

When you do x++, it is the same as x = x + 1 so it is actually a new instance of Integer assigned to x but y still points to the old instance which value is 10.

Keep in mind that wrapper instances are immutable.

Matt
  • 3,422
  • 1
  • 23
  • 28
1

For an Integer x, the expression x++ can be understood like x = new Integer(x.intValue() + 1);. Its not exactly the same, but helps for understanding.

So, it doesn't modify the Integer object with the value 10 that x pointed to before the x++, it creates a new Integer object with the value 11 independent of the original 10 Integer, and assigns this 11 Integer to x.

But there's nothing in the x++ expression that would make y point to a different Integer instance. So y still points to the original 10.

That's the difference to the CLA example where you don't introduce a new instance with ob1.val++, but modify the single instance. If the Integer class had a public field value, then x.value++ would show the behaviour you expected - but the value field is (for good reason) private - not accessible to the outside.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7