Reputation: 7
Object references in java really confuse me. And when I started to study linkedlist, I realized that I know verry little about the subject. My question is
Node node = list;
Let's say list is the first node in my linkedlist and I know that this operation means that node refers to the same object that the list refers to. I mean, if I make changes to the node, consequently to the object that it refers to, list will be also affected by these changes. However, what I don't understand that do node.next and list.next also refer to the same object. I mean if I wrote the linkedlist like
Node node = list;
node = node.next;
Does that mean I am also changing list with the list.next? Or if I write (node.next.name = "B"), do I also change the name of the list.next element?. I know the questions seems silly but I really don't know anything about references. And I also researched the subject a lot. But what I found online so far, they didn't help me much. If someone explains it to me in a clear and comprehensible way, I would be very appreciated.
Upvotes: 0
Views: 4297
Reputation: 47729
Node node
is a variable that holds a reference. The "reference" is another term for a "pointer" -- the memory address of an object.
When you assign a value to node
you're storing the address of an object in that location. The same address can be stored other places, allowing the object to be accessed from different locations in the code.
If you have a linked-list Node object, it presumably has fields for "next" and "name". The name field in this case is presumably a reference to an object (eg, a String) that is the "value" of the node (or at least part of the node's "value). The "next" field is pretty much guaranteed to typed to hold a Node reference (though the value may be null for end-of-list).
So your local node
variable may "point" to a Node object that contains a next
field that "points" to yet another Node object that contains a "next" field that points to yet another Node object, ad infinitum.
(One important thing to understand is that, just because you have a Node object with a "next" field that points somewhere does not mean that every Node object's "next" field points to that same somewhere. The name of the field is not the identity of the object.)
When you do something like node.next.name = "B";
that can be decomposed into:
Node temp = node.next;
temp.name = "B";
The "name" field in the Node that you would address with node.next is changed. node
gives you the address of the closest Node, and node.next
then gives you the address of the node next to the closest Node.
This sequence (from your original post):
Node node = list;
node = node.next;
is something you might see in code that is beginning to traverse the list. The first line initializes node
from the "list head" pointer named list
. Then the second line "advances" node to the second Node in the list. So that after this statement node
is addressing the second Node of the list.
Upvotes: 1
Reputation: 6023
You asked:
Does that mean I am also changing list with the list.next?
No, you are just reassigning the object variable (object reference) node
to point at a different node in the list.
if I write (node.next.name = "B"), do I also change the name of the list.next element?
When node
is assigned to list
then the statement you wrote is true. After node = node.next;
the behavior will modify a different node.
It helps if you keep in mind that any NODE or subset of NODES in your list, can be thought of as a list in its own right.
Hope that helps.
Upvotes: 0
Reputation: 7031
You know a reference means that in the memory, they have the same "reference" to the data in memory.
So it is as simple as node
and list
are the same object.
If you consider that the data is stored in the memory (RAM). Consider the memory as a closet full of drawers,
if the data of node
are in drawer number 2. "by Reference" means that node = list
means that list
also refers to drawer number 2.
Now whatever you change in the data, you're changing the values of drawer number 2. So you're changing both node
and list
Upvotes: 3