简体   繁体   中英

Confused about the reference in Java

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

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

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 .

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

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];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM