Vipin Vipin - 3 months ago 9
Java Question

Java variable not changed, even after reference it was pointing to got changed

I am trying to assign value in result.next but somehow value is not getting changed when i=1 iteration is finished.

Initially result is null.

First iteration when i is 0, result.next points to ListNode(10)

when i=1 has finished execution i am expecting value of result.next to ListNode(2), because it has reference of temp.

public void Mymethod() {
ListNode result = null;
ListNode temp = new ListNode(10);
int j = 0;
for (int k = 0; k < 10; k++) {
for (int i = 0; i < 2; i++) {
if (j == 0) {
j++;
result = new ListNode(1);
result.next = temp;
continue;
}
temp = new ListNode(2);
}
}
}
class ListNode {
public int val;
public ListNode next;

ListNode(int x) {
val = x;
next = null;
}
}

Answer

...because it has reference of temp.

No, it has a copy of the reference that was in temp as of the result.next = temp line. But there is no ongoing link between result.next and temp. Later when you do temp = new ListNode(2), that changes the value of temp to a new reference, which has no effect whatsoever on result.next.

After the result.next = temp line in the first iteration of your inner loop, here's (roughly) what we have in memory:

[temp: Ref11243]−−−−−−−−−−−−−−−−−−−−−−−−−−+
                                          |
                                          |
                                          |
                                          |    +−−−−−−−−−−−−+
                                          + −−>| ListNode   |
                      +−−−−−−−−−−−−−−−−+  |    +−−−−−−−−−−−−+
[result: Ref89895]−−−>|    ListNode    |  |    | val: 10    |
                      +−−−−−−−−−−−−−−−−+  |    | next: null |
                      | val: 1         |  |    +−−−−−−−−−−−−+
                      | next: Ref11243 |−−+
                      +−−−−−−−−−−−−−−−−+

Note that result and temp both contain the same value, a reference to the ListNode you created at the top (I've shown it in the above as Ref11243, but we never see or need to see the actual value).

But then when you do this:

temp = new ListNode(2);

you have this instead:

                                              +−−−−−−−−−−−−+
[temp: Ref68545]−−−−−−−−−−−−−−−−−−−−−−−−−−−−−>| ListNode   |
                                              +−−−−−−−−−−−−+
                                              | val:  2    |
                                              | next: null |
                                              +−−−−−−−−−−−−+
                      +−−−−−−−−−−−−−−−−+
[result: Ref89895]−−−>|    ListNode    |   
                      +−−−−−−−−−−−−−−−−+   
                      | val: 1         |      +−−−−−−−−−−−−+
                      | next: Ref11243 |−−−−−>| ListNode   |
                      +−−−−−−−−−−−−−−−−+      +−−−−−−−−−−−−+
                                              | val: 10    |
                                              | next: null |
                                              +−−−−−−−−−−−−+

Note how temp's value changed, so now it points to a different ListNode, the new one created with new ListNode(2).


In a comment you've asked:

How can i give some link here ? so that whenever temp is changed result.next starts pointing to new Object.

You can't, Java doesn't have any concept of references to variables, just references to objects (including arrays).

Comments