Core Java

Java : Pass By Value Or Pass By Reference

Introduction:

Java is Pass By Value or Reference is often a debated question among many Java developers. The answer to it is that Java is a Pass By Value, not Pass By Reference. In this tutorial, we’ll see how.

To follow along with this tutorial, let’s first understand these two terms:

  1. Pass By Value: The actual value is copied over to the method arguments being passed to the method for its execution.
  2. Pass By Reference: An alias to the actual value is passed to the method for its execution. Any changes in the aliased value reflect back to the original value.

This will make more sense as we go along with this tutorial. With this, let’s get started!

Initial Setup:

For this tutorial, we’ll refer to a Student class:

public class Student {
    private String name;
    
    public Student(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }

    public String toString() {
        return this.name;
    }
}

Java Is Pass By Value:

Let’s start by defining a Student object kate & invoking changeStudentReference() method on its reference:

Student kate = new Student("Kate");
changeStudentReference(kate); 
System.out.println(kate); //Prints "Kate"

where changeStudentReference() is defined as:

public void changeStudentReference(Student ref) {
    ref = new Student("James"); 
}

It is evident that on code execution, the kate reference is still referring to a Student with a name “Kate”. Why so?

It’s due to the fact that while invoking changeStudentReference(), the value of reference kate was copied over to the reference ref.

There’s a difference between passing the actual object reference or pointer to the method and passing a copy of the reference. And Java passes a copy of the reference. So in changeStudentReference() method, when we change the value of the copy of the reference, which is ref, it won’t impact the original reference kate.

To understand it further, let’s create one more Student object:

Student miranda = new Student("Miranda");

And invoke a swap() method by passing kate and miranda references to it:

swapStudents(kate, miranda);
System.out.println(kate+":"+miranda);

where swap() is defined as:

public void swapStudents(Student ref1, Student ref2) { 
    Student temp = ref1; 
    ref1 = ref2;
    ref2 = temp;
}

What do you think will be printed as an output in this case?

By now, if you have followed along, you must be clear that ref1 and ref2 are only the copies of the references kate and miranda and they will only exist within the scope of swapStudents() method.

So, our original references kate and miranda are still pointing to “Kate” and “Miranda” respectively and the output will be “Kate:Miranda”.

Hope by now, it has started making sense why we are calling Java to be a Pass By Value, the reason being the copies of references are passed to the method and not the original references.

Changing Object State:

Let’s consider what if we invoke a changeStudentName() method by passing in the reference – kate:

changeStudentName(kate);
System.out.println(kate);

where changeStudentName() is defined as:

public void changeStudentName(Student ref) { 
    ref.setName("James");
}

And here comes the catch!

What will be the output? It’ll be “James”, and not “Kate”. Why?

Though ref is only a copy of kate but still within the scope of changeStudentName() method it was also referring to a Student object with name “Kate” on the memory heap.

On invoking ref.setName() method, we altered the state of that object i.e. changed its name property to hold a value of  “James”. On completion of changeStudentName() method, ref will no longer be in scope but the changes it made to the state of an object will remain as-is.

Conclusion:

In this article, we learned that Java is Pass By Value. We also looked at some good examples to prove it so.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *