Pass By Value in Java

Let us start by understanding the terms “Pass By Value” and “Pass By Reference”.

Pass By Value(copy the value, and pass the copy):

In this case, the value of the passed in variable is copied and passed to the method. Just like cloning, the value of the variable is copied and passed in as the argument to the method. Any change to the passed in argument does not have any effect on the original variable.

Pass By Reference(pass the alias):In this case, an alias of the original variable is passed as the argument.Any change to the alias has the direct effect on the original variable.

In Java, the variables are passed by value only. There is no pass by reference concept in Java.
For primitives, you pass a copy of the actual value.
For references to objects, you pass a copy of the reference.

Let us take the example of primitive.

package com.test.primitive;

public class Primitive {

public void testMethod(int x){ // passed in variable

x=9;  // This will not affect the original variable value

System.out.println(“Inside Method:Passed-In:”+x);

}

public static void main(String[] args) {

Primitive p = new Primitive();

int x = 5; // original variable

p.testMethod(x); // Here the value of variable x i.e.. 5 is copied and passed to testMethod as argument

System.out.println(“Inside Mains:original:”+x);

}

}

Output

Inside Method:Passed-In:9
Inside Mains:original:5

Passing Object References By Value

Please note that Objects are not passed to methods. In Java, only reference of Objects are passed by value. It means, the copy of the original reference is created and passed as argument. The changes to the copy reference does not impact the original reference. But both the original and copy references can access the object and make changes to it.

Employee emp = new Employee();

screen-shot-2017-02-28-at-2-28-02-pm

In the above, emp is the reference which points to an Employee Object in Heap. A reference does not actually contain the address of the Object, but it helps JVM to locate & access the Object.

Let us take a look into the below example.

 package com.test;

public class Employee {

private String name;

public Employee(String name) {
this.name = name;
}

public String getName() {
return name;
}

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

public static void main(String[] args) {
TestEmployee testEmployee = new TestEmployee();
Employee emp = new Employee(“Gyan”);// Original reference- emp. Please see Figure 1

screen-shot-2017-02-28-at-9-42-24-pm
testEmployee.method(emp);
System.out.println(“Original:”+emp.getName());
}

}

class TestEmployee {

public void method(Employee emp){// Passed-in variable- emp(copy). Both emp(original) and emp(copy) points to the same Employee Object. Refer Figure 2

screen-shot-2017-02-28-at-9-46-26-pm
emp.setName(“Prabhakar”);//The name of the Employee is changed via emp(copy).Refer Figure 3

Screen Shot 2017-02-28 at 9.53.10 pm.png

emp = new Employee(“Vivek”);// copy reference emp(copy) is now pointing to another Employee Object. But this does not impact the original reference. The original reference still points to the previous Employee Object.Refer Figure 4

screen-shot-2017-02-28-at-9-57-38-pm
System.out.println(“Copy:”+emp.getName());
}

}

Output

Copy:Vivek
Original:Prabhakar

Leave a Reply

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