Back to Top

Sunday 15 January 2012

JAVA : What is meant by pass by reference and pass by value in Java?

Pass by reference means, passing the address itself rather than passing the value.
While, on the other hand, pass by value means passing a copy of the value.

Java, actually works as pass by value. Let’s check this with the help of 2 examples.
Examples:
1.     By passing primitive data type:

In the below code, we have written a method called doInt(), which takes a int value and decrease the value by one and printing it there. Now, from main() method, we are passing the int i of value 10, then we are printing from main().

public static void main(String[] args) {
              int i= 10;
              System.out.print("OUTPUT: ");
              doInt(i);
              System.out.print(i);
       }
      
       public static void doInt(int i)
       {
              i--;
              System.out.print(i+", ");
       }

Output:
And the output of the above code is, 9 is printed from the doInt() method, whereas, it is not reflected in main method, because, it is still printing 10.
OUTPUT: 9, 10
Therefore, the conclusion over here is, it is passing the value, instead of reference.
2.     By Passing Object:
Now, the second example, by passing an object.
Say, we have a class named, Employee, which has 2 fields strName and intAge.

class Employee{
       private String strName;
       private int intAge;
      
       public String getStrName() {
              return strName;
       }
       public void setStrName(String strName) {
              this.strName = strName;
       }
       public int getIntAge() {
              return intAge;
       }
       public void setIntAge(int intAge) {
              this.intAge = intAge;
       }
       @Override
       public String toString() {
              return "Employee [strName=" + strName + ", intAge=" + intAge + "]";
       }
      
}
Now, we will create a method (with return type void), which will set the name and age of an employee object.

public static void setEmployee(Employee objEmployee){
                          
      objEmployee.setStrName("James");
      objEmployee.setIntAge(22);
             
}
Then we will call this from a main method to test it, we
public static void main(String[] args) {

       Employee objEmployee = new Employee();
       setEmployee(objEmployee);
       System.out.println(objEmployee);
}
Output:
And, in the output we can see the value of the attributes of employee object, is what we set in the setEmployee() method.
Employee [strName=James, intAge=22]

Therefore, it looks like the reference has been passed.
Yes, but still, it follows the concept of pass by value, because, the value of  objEmployee is, the memory reference of the actual object.


Therefore, we can say, Java only supports, Pass By Value, not, Pass By Reference.

1 Responses to “ JAVA : What is meant by pass by reference and pass by value in Java? ”

Padmanaban said...
17 July 2012 at 18:14

kindly ,just explain it is in program ..


Post a Comment

Popular Posts

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar