0

According to this answer https://stackoverflow.com/a/12020435/562222 , assigning an object to another is just copying references, but let's see this code snippet:

public class TestJava {

    public static void main(String[] args) throws IOException {

        {
            Integer x;
            Integer y = 223432;
            x = y;
            x += 23;
            System.out.println(x);
            System.out.println(y);
        }
        {
            Integer[] x;
            Integer[] y = {1,2, 3, 4, 5};
            x = y;
            x[0] += 10;
            printArray(x);
            printArray(y);
        }
    }

    public static <T> void printArray(T[] inputArray) {
        for(T element : inputArray) {
            System.out.printf("%s ", element);
        }
        System.out.println();
    }

}

Running it gives:

223455
223432
11 2 3 4 5 
11 2 3 4 5 
Community
  • 1
  • 1
qed
  • 22,298
  • 21
  • 125
  • 196
  • 1
    Note that `x += 23;` is the same as `x = x + 23;`, therefore it is an assignment of the reference `x`. – Radiodef Nov 06 '14 at 21:51
  • 1
    @Radiodef - Actually, it's the same as `x = Integer.valueOf(x.intValue() + 23);` – Ted Hopp Nov 06 '14 at 22:24
  • @TedHopp You're correct it expands fully to that but my point is it is a reference assignment whether you know that unboxing and boxing happens or not. – Radiodef Nov 06 '14 at 22:37

3 Answers3

4

The behavior is consistent. This line:

x += 23;

actually assigns a different Integer object to x; it does not modify the value represented by x before that statement (which was, in fact, identical to object y). Behind the scenes, the compiler is unboxing x and then boxing the result of adding 23, as if the code were written:

x = Integer.valueOf(x.intValue() + 23);

You can see exactly this if you examine the bytecode that is generated when you compile (just run javap -c TestJava after compiling).

What's going on in the second piece is that this line:

x[0] += 10;

also assigns a new object to x[0]. But since x and y refer to the same array, this also changes y[0] to be the new object.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

Integer is immutable, so you cannot modify its state once created. When you do this:

Integer x;
Integer y = 223432;
x = y;
x += 23;

The line x += 23 is assigning a new Integer value to x variable.

Arrays, on the other hand, are mutable, so when you change the state of the array e.g. changing one of the elements in the array, the other is affected as well.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • -0.5. I find this answer rather misleading. The difference in behaviour isn't to do with whether an `Integer` or an array is mutable or immutable - it's due to the fact that the assignment in the first block assigns something to `x` and the assignment in the second block does not. The immutability of `Integer` objects is a complete red herring. – Dawood ibn Kareem Nov 06 '14 at 21:51
  • @DavidWallace since `Integer` is immutable, you do not change its state, but always create a new instance and assign it. That's why it comes into relevance and why doing `x += 23` creates a new instance rather than modifying the current state of the variable. – Luiggi Mendoza Nov 06 '14 at 21:52
  • But you could do the same kind of thing with a mutable class and see the same effect. – Dawood ibn Kareem Nov 06 '14 at 21:53
  • @DavidWallace if you do `x = new Thatclass(...)` which may break OP's intention. – Luiggi Mendoza Nov 06 '14 at 21:54
0

When you do this x += 23;, you actually create another object, and you made x point on this new object. Therefore, x and y are 2 different objects.

ToYonos
  • 16,469
  • 2
  • 54
  • 70