Confused about the reference in Java

2392 views java
0

I am confused about why the value of next is not the same as the root.children[0]? In my understanding, next point to the root.children[0]. Therefore, if the value of root.children[0] is changed, next should know.

public class MyClass {
    public static void main(String args[]) {
        Node root = new Node();
        Node next = root.children[0];
        root.children[0] = new Node();
        System.out.println(root.children[0]);
        System.out.println(next);
    }

    public static class Node {
        Node[] children = new Node[1];
    }
}

output

MyClass$Node@e6ea0c6
null

answered question

next does not point to root.children[0], it's a copy of root.children[0] and therefore references the same object as root.children[0] for as long as root.children[0] is not changed.

When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same

you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line

3 Answers

1

Consider it like this, I'll mark the object in memory as {} and the reference as -> So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.

Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing

If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node} and next will NOT point to this newer node

When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object

posted this
11

Let's dissect this code line by line:

Node root = new Node();

You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.

Node next = root.children[0];

As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.

root.children[0] = new Node();

Now children[0] is being assigned a non-null value. Note that this does not change the value of next.

posted this
12

Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.

public class MyClass {
    public static void main(String args[]) {
        Node root = new Node();
        root.children[0] = new Node();
        Node next = root.children[0];
        System.out.println(root.children[0]);
        System.out.println(next);
    }

    public static class Node {
        Node[] children = new Node[1];
    }
}

posted this

Have an answer?

JD

Please login first before posting an answer.