Pass by Value and Pass by Reference in Java for Method Parameters

What is Pass by Value and Pass by Reference?

Pass by value and pass by reference or call by value and call by reference are two mechanisms to pass parameters to methods in a programming language. In pass by value mechanism method is passed the value provided by the caller, therefore, changes made to the formal parameters of the method have no effect on the original values that were passed by the caller. On the contrary, pass by reference means that the method gets the location of the variable that the caller provides. Thus, changes made to the formal parameters will affect the actual arguments used to call the method.

Regardless of pass by value and pass by reference there are two terms actual and formal parameters related to method parameters. Formal parameter is an identifier used in a method to stand for the value that is passed into the method by a caller. Actual parameter is the actual value that is passed into the method by a caller. To avoid confusion, it is common to view a formal parameter as a variable, and an actual parameter as a value. Some authors use the term parameter for formal parameter, and the term argument for actual parameter.

Java uses Pass by Value (Call by Value)

Java programming language always uses pass by value. In Java, all parameters to methods are pass by value or pass by value. Cay S. Horstmann and Garry Cornell have mentioned in their very famous book "Core Java Volume - I Fundamentals" that the Java programming language always uses pass by value. That means the method gets a copy of all parameter values, and the method cannot modify the contents of any parameter variables that are passed to it. Java uses two kinds of method parameters:

  • Java primitive types
  • Java object references

It looks very straight forward and simple when you experiment passing primitive types to a method but becomes obscure when it comes to passing objects to a method. Interestingly, when an object reference is passed to a method, the method gets a copy of the object reference, and both the original and the formal copy refer to the same object, therefore within from the method the state of an object parameter can be changed.

But do not get confused by changing the state of the object which is being pointed by the reference and the value of the reference. Suppose you have a reference ref that points to some object in memory, and you pass this reference ref to some method and ref gets copied into a formal parameter formalRef. Now, if you assign null to formalRef inside the method then only formalRef will be nullified, the actual reference ref will still keep pointing to the same object in memory. Let's see the following piece of code (CallByValueDemo.java)and in line comments to understand the idea of pass by value in Java:

/* CallByValueDemo.java */
 
class Student
{
    String name;
    int rollno;
 
    Student()
    {
        name = null;
        rollno = 0;
    }
 
    Student (String name, int rollno)
    {
        this.name = name;
        this.rollno = rollno;
    }
}
 
public class CallByValueDemo
{
    public static void main(String[] args)
    {
        Student s1 = new Student("Garvita", 10);
        Student s2 = new Student("Anushka", 11);
 
        System.out.println("s1 - Name: " + s1.name + "\t" + "Roll No: " + s1.rollno);
        System.out.println("s2 - Name: " + s2.name + "\t" + "Roll No: " + s2.rollno);
        System.out.println(); //inserting new line
 
 
        /* swap (s1, s2) would not be able to swap 
         * objects s1, and s2 because in Java, parameters 
         * are passed by value to the function.		
         */ 
        swap (s1, s2);
 
        System.out.println(); //inserting new line
        System.out.println("s1 - Name: " + s1.name + "\t" + "Roll No: " + s1.rollno);
        System.out.println("s2 - Name: " + s2.name + "\t" + "Roll No: " + s2.rollno);
    }
 
    static void swap (Student x1, Student x2)
    { 
        /* objects x1, and x2 are swapped but 
         * they will have no impact on actual 
         * parameters passed to this function.
         * Therefore, x1, and x2 will be swapped 
         * but not s1, and s2 that are actual parameters
         * passed to this function.
         */ 
        Student temp = x1;
        x1 = x2;
        x2 = temp;
 
        //Print swapped content of x1, x2
        System.out.println("From swap method x1 - Name: " + x1.name + "\t" + "Roll No: " + x1.rollno);
        System.out.println("From swap method x2 - Name: " + x2.name + "\t" + "Roll No: " + x2.rollno);
    }
}
 
OUTPUT
------
s1 - Name: Garvita	Roll No: 10
s2 - Name: Anushka	Roll No: 11
 
From swap method x1 - Name: Anushka	Roll No: 11
From swap method x2 - Name: Garvita	Roll No: 10
 
s1 - Name: Garvita	Roll No: 10
s2 - Name: Anushka	Roll No: 11

Read the above piece of code carefully, you will find that method swap() within from the main() method is passed two objects s1 and s2 those are received by swap() in x1 and x2 references respectively. Then inside swap() method x1 and x2 are interchanged. By doing so x1 holds the object earlier referred by x2 and x2 does the opposite. In this process of interchanging formal parameters x1 and x2 there would be no impact on s1 and s2, which states that in Java arguments are passed by value not by reference. That's how Java handles pass by value and pass by reference mechanisms of passing parameters to methods.

But as we have articulated earlier if you pass a variable of a class type to a method, you are able to access the members of actual argument passed to a function within from the function with help of formal parameters. This effectively means that the state of the object can be changed within from the function. Of course, following things you can and cannot do with method parameters in the Java programming language:

  • A method cannot modify a parameter of primitive type (that is, numbers or boolean values).
  • A method can change the state of an object parameter.
  • A method cannot make an object parameter refer to a new object.

Last Word

In this tutorial we talked of pass by value and pass by reference or pass by value and pass by reference mechanisms of parameter passing to Java methods. We saw that Java uses pass by value (call by value). Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

References



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.