Tuesday, February 7, 2012

Call by value or Call by reference in Java

Call by value or Call by reference in Java

In Java:
Call By value - Premitive types passed in the method call, push their values on stack and hence said as called by Values.
                So once call is returned from method, all values on STACK has gone vanished.

Call By Reference - Objects passed to any method call, pass their reference on stack and hence said as called by reference.

Remiaining is Call by Address (should be same as call by reference), if not, the person may be addressing following:
     Static final member variables (constants) passed in the method call which are in the permanent memory 
     and only the address (or reference) is pushed on stack. As the value on this address can not be changed (only read), 
     the person is refering it as address and not reference.

In Java, it is "call by value". 
If you pass any object reference for swapping in Java (since it is call by value), swapping fails. I mean it is useless once
method call is ended. Here Java copies the reference and sends to method as value. So if you do some change in attribute 
of passed Object, then only the original object changes.

====In the below code, I have written two methods for swapping "swapObjects" and "modifyAndSwapObjects". Check carefully:
//Employee.java
public class Employee {
    int x=0;
    public Employee(String name){
        x=name.length();
    }
}

//CallByValue.java : Main Class
public class CallByValue {
    public static void main(String[] args) {
        Employee rajeev = new Employee ("rajeev") ;
        Employee mohan = new Employee("mohan") ;
        System.out.println("Before:"+rajeev+", rajeev.x:"+rajeev.x);
        System.out.println("Before:"+mohan+", mohan.x:"+mohan.x);
        
        new CallByValue().swapObjects ( rajeev, mohan);
        System.out.println("After swapObjects:"+rajeev+", rajeev.x:"+rajeev.x);
        System.out.println("After swapObjects:"+mohan+", mohan.x:"+mohan.x);
        
        new CallByValue().modifyAndSwapObjects ( rajeev, mohan);
        System.out.println("After modifyAndSwapObjects:"+rajeev+", rajeev.x:"+rajeev.x);
        System.out.println("After modifyAndSwapObjects:"+mohan+", mohan.x:"+mohan.x);
    }

    public void swapObjects (Employee x, Employee y){
        Employee temp = new Employee (" ");
        temp = x;
        x = y;
        y = temp;   //This swapping will not make any difference after method call ends.
    }
    public void modifyAndSwapObjects (Employee x, Employee y){
        Employee temp = new Employee ("Raj");
        x.x=20;   //Attribute is changed and hence this value will persist for X object.
        temp = x;
        x = y;
        y = temp;
    }
}
//Output:
Before:Employee@164be2, rajeev.x:6
Before:Employee@164be4, mohan.x:5
After swapObjects:Employee@164be2, rajeev.x:6              //Even after swap, lenght is still 6 and Hashcode is the same.
After swapObjects:Employee@164be4, mohan.x:5               //Even after swap, lenght is still 5 and Hashcode is the same.
After modifyAndSwapObjects:Employee@164be2, rajeev.x:20    //Here attribute is changed, so value persist.
After modifyAndSwapObjects:Employee@164be4, mohan.x:5      //Another object's attribute is not changed, so no change in value.

===============================END===============================

No comments:

Post a Comment