-6

I am reposting my question.

There are 3 reference variables root1,root2,root3,they are included in a reference array (root[]={root1,root2,root3}).

I initialized the object for root[0],I hope root[0] refers to root1 ,So i expect root1.data and root[0].data to access the same value.But i am not able to access using root1.data whereas i am able to access it using root[0].data.

public class testing 
{
static Node root1,root2,root3;
    public static void main(String[] args)
    {
        Node root[]={root1,root2,root3};
        root[0]=new Node(2,null);
        System.out.println("root[0]="+root[0].data);//It is working properly
        System.out.println("root1="+root1.data);//It is not working properly
    }
    public static class Node
    {
        int data;
        Node next;
        Node(int data,Node next)
        {
            this.next=next;
            this.data=data;
        }    
    }
}
Ramgopal
  • 21
  • 9

3 Answers3

0

No they are not supposed to access the same value,

After initializing your array, you simple put in each node a null value

     root[0]=new Node(null,2);

The new created node will be referenced by the first node in the array , array1 still has value of null

         root1=new Node(null,3);

now root1 one is pointing to the new node

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
0

Variables and array elements are all references to objects. They are not the actual objects.

To illustrate, I'll "name" actual objects using a number scheme, e.g. #1 is the first object.

Your code runs as follows:

  • Class is initialized by the JVM, and root1, root2, and root3 are all null.

  • Node root[]={root1,root2,root3} is executed and it creates a Node[] object (#1) with length of 3, and copies the null values of root1, root2, and root3. The code is equivalent to:

    Node[] root = new Node[3];
    root[0] = root1;
    root[1] = root2;
    root[2] = root3;
    
  • root[0]=new Node(null,2) is executed and creates a Node object (#2) and assigns it to root[0]. Note that root1 remains null.

  • root1=new Node(null,3) is executed and creates a Node object (#3) and assigns it to root1. Note that root[0] still references #2.

  • You print the value of root[0].data, and since root[0] references #2, it will print 2.

  • You print the value of root1.data, and since root1 references #3, it will print 3.

Everything works the way it should.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

Have a look at the simplified code that has the same behavior:

public class Testing2 {
    public static void main(String[] args) {
        Node r1 = new Node(1);
        Node r2 = r1;
        r1 = new Node(2);

        System.out.println("r1=" + r1.data);// prints 2
        System.out.println("r2=" + r2.data);// prints 1
    }

    public static class Node {
        int data;

        Node(int data) {
            this.data = data;
        }
    }
}

It is important to understand that r1 and r2 are pointing at Node objects. In some languages r1 and r2 would be called pointers, in Java they are called references.

When you execute:

Node r1 = new Node(1);

You have r1 pointing at the instance of Node with data = 1

r1 -> Node.data=1

After

Node r2 = r1;

You have both r1 and r2 pointing at the same Node instance

r1 -> Node.data=1 <- r2

Finally after

r1 = new Node(2);

You have

        Node.data=1  <- r2
r1 ->   Node.data=2 

Which explains the output of the program.

David Soroko
  • 8,521
  • 2
  • 39
  • 51