2

I have a beginners question about java fundamentals of variable assignments.

In my example code I have 3 boxes (Objects). I assign the boxes as follows:

    Box box1 = new Box("Furniture", 1);
    Box box2 = new Box("Games", 2);
    Box box3 = new Box("Cloths", 3);

    box1 = box2;
    box2 = box3;

    System.out.println(box1.toString());
    System.out.println(box2.toString());

Now, I would expect that box1 also points to box3. But it turns out, that it still points to box2, eventhough I also changed the reference of box2 to box3. Why is that so?

Phil
  • 23
  • 3
  • 2
    `box1 = box2;` copies the *current value of `box2`* as the new value of `box1`. It doesn't permanently link the variables. Any further change to the value of `box2` is irrelevant. Note that the values of these variables aren't objects, they're references - which can confuse things sometimes. Does that help? – Jon Skeet Apr 04 '19 at 16:56
  • Right, this isn't really different from copying, say, integer values around. Try the same thing but with three `int`s instead, you might get it better. – markspace Apr 04 '19 at 16:57
  • [Read this](https://stackoverflow.com/a/40523/407953) - Java is always works with pass-by-value, although in case of Object this is a value of a reference – igorepst Apr 04 '19 at 17:02
  • Thank you - I expected the assignment of box1 to box2 as "updatable". But I understand now that box1=box2 only assignedthe current value of box2, irrespective of what happens to box2 afterwards. – Phil Apr 04 '19 at 17:21

2 Answers2

8

This is your initial state :

     +-----------------+             +----------------+
     |  box1 ( ref )   +------------>|  box1 ( obj )  |
     +-----------------+             +----------------+

     +-----------------+             +----------------+
     |  box2 ( ref )   +------------>|  box2 ( obj )  |
     +-----------------+             +----------------+

     +------------------+            +----------------+
     |  box3 ( ref )    +----------->|  box3 ( obj )  |
     +------------------+            +----------------+

This is what happens after box1 = box2 :

     +-----------------+             +----------------+
     |  box1 ( ref )   +----+        |  box1 ( obj )  |
     +-----------------+    |        +----------------+
                            |
     +-----------------+    +------> +----------------+
     |  box2 ( ref )   +------------>|  box2 ( obj )  |
     +-----------------+             +----------------+

     +------------------+            +----------------+
     |  box3 ( ref )    +----------->|  box3 ( obj )  |
     +------------------+            +----------------+

This is what happens after box2 = box3

     +-----------------+             +----------------+
     |  box1 ( ref )   +----+        |  box1 ( obj )  |
     +-----------------+    |        +----------------+
                            |
     +-----------------+    +------> +----------------+
     |  box2 ( ref )   +----+        |  box2 ( obj )  |
     +-----------------+    |        +----------------+
                            |
     +------------------+   +------->+----------------+
     |  box3 ( ref )    +----------->|  box3 ( obj )  |
     +------------------+            +----------------+

Now you should be able to figure out why the output is like that . :)

Gautam
  • 1,862
  • 9
  • 16
0

See if the following makes any more sense to you.

int box1 = 1;
int box2 = 2;
int box3 = 3;

box1 = box2;
box2 = box3;

System.out.println(box1);
System.out.println(box2);

box1 prints "2" and box2 prints "3". This is exactly the same way copying references works, and it'll always have the same pattern.

markspace
  • 10,621
  • 3
  • 25
  • 39