Wednesday, 22 April 2015

Java pass by value or pass by reference

Java's parameter passing is pass by value or pass by reference is one question which causes a bit of confusion so let's try to clarify it.
Before going into whether java is pass by value or pass by reference let's first see what these 2 terms actually means.

  • Pass by value - In this case actual parameter is evaluated and its value is copied into memory(stack) used by the parameters of the method.
  • Pass by reference - In this case the memory address of the actual parameter is copied in the parameter of the method. Thus anytime method is using its parameter it is actually using the actual parameter.

In Java at least there is no confusion with primitive values as it is easy to see that primitives are passed by value.
Let's see it with an example -

public class A {
 
 void displayData(int i){  
  //value of the passed primitive variable changed
  i++;
  System.out.println("Value of i inside method "  + i);
 }  
 public static void main(String args[]){ 
  A a = new A();
  int i = 5;
  // Sending variable i as param
  a.displayData(i);  
  System.out.println("Value of i inside main "  + i);
 }  
}

Output of the program

Value of i inside method 6
Value of i inside main 5

Thus it can be seen that the changing of the passed int value in the method doesn't affect the original value.

With object passed as a parameter, people have some confusion whether it's pass by value or pass by reference. Before going it to it let's be very clear that in java every thing is pass by value whether primitives or objects. It does make sense as java has no pointers thus no way to send memory addresses of the variables.

But still if an object is passed as parameter in java and some variable in that object is changed in the method that change is global i.e. same value is reflected every where.
Let's see an example

public class A {
 private int i;
 //constructor
 A(int i){
  this.i = i;
 }

 void displayData(A obj){ 
  // object manipulated 
  obj.setI(7);
  System.out.println("Value of i inside method "  + obj.getI());
 }  
 public static void main(String args[]){ 
  A a = new A(5);
  System.out.println("Value of i inside main before method call "  + a.getI());
  a.displayData(a);  
  System.out.println("Value of i inside main after method call "  + a.getI());
 }
 //getter
 public int getI() {
  return i;
 }
//setter
 public void setI(int i) {
  this.i = i;
 }  
}

Output of the program

Value of i inside main before method call 5
Value of i inside method 7
Value of i inside main after method call 7

It can be seen from the output that changes done in the method displayData() are reflected globally. When value of i is printed after the method call it is changed. If java is pass by value then why that?

It is because when we say
A a = new A();

The new operator allocates memory for the created object and returns a reference to that memory which is then assigned to the variable of the class type.
So, it can be said “a” is a variable which is holding a value and that value happens to be the reference to the memory address.

When the method is called with
a.displayData(a);

copy of the reference value is created and passed into the other object which means in method void displayData(A obj) obj parameter holds the copy of the reference value thus a and obj both are references to the same memory given to the object. Refer to the image to have clarity.

pass by value or pass by reference

So to make it clear, in the case of object Java copies and passes the reference by value, not the object. That is why any manipulation of the object in the method will alter the object, since the references point to the original objects.

Let's write a program to see this statement "In the case of object Java copies and passes the reference by value" in detail.

public class A {
 private int i;
 A(int i){
  this.i = i;
 }
 void displayData(A obj){
  // change the reference
  obj = new A(8);
  System.out.println("Value of i inside method "  + obj.getI());
 }  
 public static void main(String args[]){ 
  A a = new A(5);
  System.out.println("Value of i inside main before method call "  + a.getI());
  a.displayData(a);  
  System.out.println("Value of i inside main after method call "  + a.getI());
 }
 
 // getter
 public int getI() {
  return i;
 }
 // setter
 public void setI(int i) {
  this.i = i;
 }  
}

In this program the reference of the object which was copied and passed as value to the parameter in method displayData() has been changed
obj = new A(8);
but it can be seen from the output that it doesn't change the original reference as it retains the original value of the variable i.

Value of i inside main before method call 5
Value of i inside method 8
Value of i inside main after method call 5

I hope I am able to make it clear that in Java, whether primitives or objects, everything is pass by value. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Why main method static in Java
  2. Why file name and class name should be same in Java
  3. Constructor in Java
  4. static in Java
  5. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

6 comments:

  1. Brilliantly explained

    ReplyDelete
  2. The Java Spec says that everything in Java is pass-by-value . There is no such thing as "pass-by-reference" in Java. Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners . More on....pass by reference or pass by value

    Ling

    ReplyDelete