DEV Community

Chhavi Joshi
Chhavi Joshi

Posted on

Objects and reference in java

While I was stumbling over LinkedList, I came across a small thought that actually helped me understand something much bigger about Java.

I was writing an insertAtIndex() method and had this line:

Node newNodeNext = temp.next;

The day before when i had originally wrote the method understanding the concept, the same thing was quite clear but while revising i doubted it.
Example: consider a linkedlist 1 -> 2 -> 4 -> 5

now you want to add a node with value 3 at index 2 (i.e. before 4), what would be my steps:
-first i'll store the node with value 4.
-then i'll create a new node with value 3.
-then i'll point the node at index 1 (i.e. 2) to the new node (3).
-and then i'll point the new node 3 to the node i created to store value of 4.

this way the new linkedlist would be 1 -> 2 -> 3 -> 4 -> 5

but then i questioned my logic that when i'm creating a node that stores value of 4 isn't a whole new node being created and if i'm pointing to it wouldn't i lose all the elements after it. But then i realised that "new" keyword wasn't used while creating the new node, so basically i just made a new reference named newNodeNext which actually points to the same node.

For example, if:

temp.next → Node(30)

then after:

Node newNodeNext = temp.next;

both temp.next and newNodeNext are pointing to the exact same Node.

But when I do:

Node node = new Node(30);

a completely new Node object is created, even though it contains the same value.

And this whole reference thing made me think about String.

that unlike primmitive datatypes String is also a class and then the whole concept of storage of Strings lined up. It had taken me a while to realize how strings are stored and that understanding made it easier to clarify this doubt relating to follow:

String a = new String("hello");
String b = a;

then a and b are pointing to the same String object.

But if I do:

String a = new String("hello");
String b = new String("hello");

then two different String objects are created - even though their content is the same.

And this is where String Pool makes things a little interesting.

If I write:

String a = "hello";
String b = "hello";

Java can reuse the same String from the String Pool, so both references can point to the same object.

This helped me connect the dots with a normal class as well.

Suppose I have:

Student s1 = new Student();
Student s2 = s1;

I haven't created another Student here.

I have simply copied the reference, so both s1 and s2 point to the same Student object.

But:

Student s1 = new Student();
Student s2 = new Student();

creates two separate Student objects.

So the comparison that finally made sense to me was:

  • LinkedList Node → Node b = a means both references point to the same Node.
  • String → String b = a also means both references point to the same String, while new String() explicitly creates another object.
  • Normal object → Student b = a again means the reference is copied, not the object.

The important thing I took away is that I shouldn't think of:

Student s2 = s1;

as "copying the object".

It is actually copying the reference to the object.

And honestly, understanding this through LinkedList made the concept much easier for me than just reading "Java is pass-by-value and objects are reference types" somewhere.

Sometimes one confusing line of code is enough to connect a bunch of concepts together. :)

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.