This code:
Node next = null;
declares a variable of type Node . Since Node is a class, the value of next always a reference - either to an object of type Node , or to a subclass, or to a null reference, which does not apply to any object at all. and in this case, the variable starts with null .
It is very important to understand that the value of next never a Node object ... it is only ever a reference. So, suppose we have:
Node next = new Node(10); Node foo = next;
Here next and foo are separate variables, each of which has independent values ββ... but we have assigned the value of next as the initial value of foo , which means that they both refer to the same object. Therefore, if we type foo.data , it will be 10.
I like to think of variables as separate papers, and in the case of variable reference types, what is written on a piece of paper is the address of the house, or the word "zero". If the same address is indicated on two sheets of paper, they refer to the same house, but the two pieces of paper themselves are independent. Changing the value of one variable (crossing the current address and recording another) does not change anything with respect to another variable ... but changes in the house itself (for example, the door paint is red) are visible regardless of what sheet of paper you use to get there.
Please note that in your question, you included String with int , double and boolean ... but whereas int , double and boolean are primitive types (where the value of the variable is just the data itself - number, etc.), String is a class, so it is a reference type. The value of the string variable is not the text itself, but a link.
source share