Thursday, November 20, 2014

CallBy Value vs CallBy Reference

Java is call by value or call by reference. This question is asked very frequently in java interviews and can get very confusing if you do not know the concept of java programming language. As a rule of thumb always remember that java is always call be value irrespective of passing a primitive or passing an object. In case of primitive its very clear Lets try to demonstrate this by an example code.


package com.sanjiv.miscellaneous;

public class PassByValueOrReference {

public static void main(String[] args) {

int x = 10;
System.out.println("Before function call: " +x);
add(x);
System.out.println("After function call: " +x);
}

private static void add(int x) {
x = x + 5;

System.out.println("Value inside the function:" +x);

}


The output of the above program is :-

Before function call: 10
Value inside the function:15
After function call: 10

Which is very obvious. In java primitives are stored in stack and each method has its own stack. Here there will be two stack created one for main stack and the other one for add stack. When we call the add method add stack will have its own copy of int x. So whatever changes it makes to its own stack will not be reflected to the variable declared in main stack.

Things get a bit tricky when we pass an object as parameter. Lets try to demonstrate through code example.

package com.sanjiv.miscellaneous;

public class PassByValueReference {

/**
* @param args
*/
public static void main(String[] args) {
Employee acutalEmp;//1
acutalEmp = new Employee("Sanjiv");//2
System.out.println("Name before method call: " +acutalEmp.getName());
modify(acutalEmp);
System.out.println("Name after method call: " +acutalEmp.getName());
}
private static void modify(Employee formalEmp) {//3
formalEmp.setName("Kumar");//4
formalEmp = new Employee("Rajeev");//5
}



}


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

public String getName() {
return name;
}

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

The output of the above code is:-

Name before method call: Sanjiv
Name after method call: Kumar


Here whatever change we made to the employee object inside the modify() method is reflected in the main method as well. This creates the illusion that java passes object by reference which is not true.

Lets try to explain this behavior for clear understanding. I have put some marker in the code to explain it in detail. As said earlier both the methods-main and modify will have its own call stack. Java stores objects in Heap and not stack. Keep this point always in your mind. At line 1 we are creating a reference variable for Employee object. At this point no object is created. actualEmp reference is local to main method call stack. At line two we are creating a Employee object which naturally gets created on the heap. Suppose this object has a memory address 1000X assigned to it. So our actualEmployee reference is poining to 1000X memory location which holds the object. At line 3 the modify method formalEmp declares its own reference which again is local to modify method stack. But both actualEmp and formalEmp are pointing to the same memory location which is 1000X. So whatever change we are making at line 4 will be reflected in all the references that are pointing to the 1000X memoery location, which in this case is acutalEmp and formalEmp reference. To clarify things i have crated a new object at line 5. Suppose this time a new memory location of 2000X is assigned to the Employee object referenced by formalEmp reference. So after the execution of line 5 formalEmp starts pointing to 2000X memory location whereas the actualEmp reference is still pointing to 1000X. Had this been call by reference both would have pointed to 2000X memory location which is not the case. After the execution of line 5 any change made to the employee object will not be reflected in the actualEmp reference as both of them are now pointing to two different memory locations - 1000X and 2000X respectively.

Hope this explains the fact that java is always call by value and not call by reference.

Please feel free to post your comments and suggestions.




No comments:

Post a Comment