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 
3
  • 1
    Note that x += 23; is the same as x = x + 23;, therefore it is an assignment of the reference x. Commented Nov 6, 2014 at 21:51
  • 1
    @Radiodef - Actually, it's the same as x = Integer.valueOf(x.intValue() + 23); Commented Nov 6, 2014 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. Commented Nov 6, 2014 at 22:37

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

-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.
@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.
But you could do the same kind of thing with a mutable class and see the same effect.
@DavidWallace if you do x = new Thatclass(...) which may break OP's intention.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.