Understanding Call By Value and Call By Reference in Java

Introduction

Since now you have learnt about creating objects of a Class in Java, we need to checkout how we can pass arguments while creating methods.

In call-by-value approach, the value of an argument is copied to the formal parameter of the subroutine. The changes made to the parameter of the subroutine have no effect on the argument.

In call-by-reference approach, a reference to an argument is passed to the formal parameter of the subroutine, inside the subroutine, the reference is used to access the actual argument specified in the call. This means that changes made to the parameter will affect the argument used to call the subroutine.

We will explain both of the ways of passing argument to a method in Java with these examples.

Program for Call By Value

class Employee{
  double salary;
  int age;
  void incrementSalAndAge(double sal, int a){
    salary = sal+10000;
    age = a+7;
    System.out.println("Salary on call: "+salary);
    System.out.println("Salary on call: "+age);
  }
}

class CallByValueDemo{
  public static void main(String args[]){
    Employee employee = new Employee();
    double salary = 34521.86;
    int age = 23;
    System.out.println("Salary before call: "+salary);
    System.out.println("Salary before call: "+age);
    employee.incrementSalAndAge(salary,age);
    System.out.println("Salary after call: "+salary);
    System.out.println("Salary after call: "+age);
  }
}

Output

call_by_value

Program for Call By Reference

class Employee{
  double salary;
  int age;
  void incrementSalAndAge(Employee emp){
    salary = emp.salary+10000;
    age = emp.age+7;
    System.out.println("Salary on call: "+emp.salary);
    System.out.println("Salary on call: "+emp.age);
  }
}

class CallByRefDemo{
  public static void main(String args[]){
    Employee employee = new Employee();
    employee.salary = 34512.37;
    employee.age = 23;
    System.out.println("Salary before call: "+employee.salary);
    System.out.println("Salary before call: "+employee.age);
    employee.incrementSalAndAge(employee);
    System.out.println("Salary after call: "+employee.salary);
    System.out.println("Salary after call: "+employee.age);
  }
}

Output

call_by_reference

Explanation of the Java Code & Output

If you see the first program, you will find we have written a method incrmentSalAndAge() where we are sending values of the arguments to the formal parameters of the same method. Before calling incrmentSalAndAge(), we have printed the values of the arguments which will be actually passed. Then we have updated the values of the arguments in incrmentSalAndAge() method and printed.

You can see there it will print the updated values of the formal parameters. After returing from subroutine, these updated values will no longer be in existence. And it will print the values of the arguments which was stored within the arguments before calling incrmentSalAndAge() . In conclusion we can say that changes made to the parameter of the subroutine have no effect on the argument.

Now, in the second program you will see instead of passing values stored in the arguments we have passed the reference of the object to incrmentSalAndAge() method.  Thus when you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. From the output you can easily make that out. Before calling incrmentSalAndAge() we have printed values of the arguments.It has printed the values with which arguments are initialized.

During invocation of the method incrmentSalAndAge(), the method is supposed the update the values of the formal parameters and get the updated value printed. After coming back from the call it has printed the updated values for the arguments. Therefore, changes made to the parameter will affect the argument used to call the subroutine because both argument and formal parameter both are referring to the same object.

Remember, these concepts are very useful to understand Copy Constructors in Java.

Checkout more useful tutorials and definitive guidelines on Java programming here.

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.

1 Comment

Click here to post a comment