Java, two objects, object1 = object2 = class / type ... don't understand

I have two instance variables, head and tail. There is a line in the code:

head = tail = new Node<E>(); 

Does this mean that there are two instances, head and tail, of the Node class? I am very confused here.

+4
source share
8 answers

It just means:

 tail = new Node<E>(); head = tail; 

So, there are 2 links ( head and tail ) pointing to the same instance of Node<E> .

+13
source

This means that there are two references to ONE Node object.

Line tail = new Node<E>(); actually returns a value (in this case, an object reference) equal to the assigned value.

+4
source

No, there is only one instance of Node<E> , but both head and tail refer to it, so you have two reference variables pointing to the same object.

+3
source

Only one instance of Node . Both head and tail refer to the same instance.

+2
source

No, of course not.

Here's what happens in this code, in sequence.

  • 'new' is used to create an instance, as an object, of a Node class
  • reference to this instance is stored in the tail description
  • the link to this instance is stored in the original link.
+2
source

2 head and tail links are assigned to the same Node instance.

+1
source

Only one object is created, the head and tail refer to the same object.

+1
source
 object1=object2 ; 

Here Object1 one refrence to other means just object2 copies all the addresses into the link object1

Just object2 is copied to object1

0
source

All Articles