Tuesday, January 17, 2012

Understanding Call By Reference And Call By Value In JAVA


In java we usually pass parameters to the function but sometimes it works out to be call by value and sometimes call by reference. This behavior is dependent on what parameters are getting passed to the function.Unlike C where we choose its call by reference or value.So below are set of rules you can keep in mind to decide whether the code will implement call by value or call by reference.

Simplified Rule:
Whenever we pass any variable belonging to primitive it is always passed as pass by value. Whenever we pass object of the class it mostly happens as pass by reference except certain circumstances.Below example 
public class Example {
 public class Try {
 public static void main(String[] args) {
  int i = 0;
  String s = "In Main:String";
  StringBuffer sb = new StringBuffer("In Main:StringBuffer");
  System.out.println("**********************************************");
  System.out.println("Value of i in main before calling function i=" + i);
  primitve(i);
  System.out.println("Value of i in main after calling function i=" + i);
  System.out.println("**********************************************");
  System.out.println("Value of StringBuffer sb in main before function ="+sb);
  stringBufferProcess(sb);
  System.out.println("Value of StringBuffer sb in main before function ="+sb);
  System.out.println("**********************************************");
  System.out.println("Value of s in main before calling function s=" + s);
  stringProcess(s);
  System.out.println("Value of s in main after calling function s=" + s);
 }

 public static void stringBufferProcess(StringBuffer c) {
  System.out.println("Value of StringBuffer inside function before processing="+c);
          c.append(":::stringBufferProcess Func:::");
          System.out.println("Value of StringBuffer inside function ="+c);
 }

 public static void primitve(int i) {
  System.out.println("Value of i in function before processsing i=" + i);
  i = i + 5;
  System.out.println("Value of i in function after processsing i=" + i);
 }

 public static void stringProcess(String s) {
  System.out.println("Value of s in function before processsing s=" + s);
  s = s + "In StringProcess:";
  System.out.println("Value of s in function after processsing s=" + s);
 }

}

The above program would generate some output like this:
**********************************************
Value of i in main before calling function i=0
Value of i in function before processsing i=0
Value of i in function after processsing i=5
Value of i in main after calling function i=0
**********************************************
Value of StringBuffer sb in main before function =In Main:StringBuffer
Value of StringBuffer inside function before processing=In Main:StringBuffer
Value of StringBuffer inside function =In Main:StringBuffer:::stringBufferProcess Func:::
Value of StringBuffer sb in main before function =In Main:StringBuffer:::stringBufferProcess Func:::
**********************************************
Value of s in main before calling function s=In Main:String
Value of s in function before processsing s=In Main:String
Value of s in function after processsing s=In Main:StringIn StringProcess:
Value of s in main after calling function s=In Main:String 
Explanation:

When we pass a primitive it will be pass by value so change in value of i is not reflected in main. When we pass object reference to the function  object get modified.However this is only true till the object passed is mutable.If the object passed in immutable it becomes pass by value. In above example behave objects of 2 classes String Buffer and String but both behave differently.
This is mainly due to the fact that object of String class is immutable whereas object of StringBuffer class is mutable ie. Immutable objects are the objects whose state can not be changed once constructed. whereas object of StringBuffer class is mutable.

Conclusion:
1)When we pass a primitive to a function it is call by value
2) When we pass object as parameter to the function and if object is immutable then it is pass by value.
3) When we pass object as parameter to the function and if object is mutable then it is pass by reference.


Clarification:
Java dosent have separate call by reference or call by value. I agree with it. The post just tries to draw lines with C. and tries to explain when you pass object as parameter to function when the value is reflected back in calling function and when it is not.


I think this explaination is clear



No comments: