Saturday 14 January 2017

Java interview question:Pass by value or Pass by reference ?

By looking at the title you may think this is just another article related to java object reference management. But in this article I want to share my own experience which was very surprising for me also. I have asked the same question to many experienced people in interview but surprisingly 7 out of 10 people gave wrong answer for this very basic but important concept of core java.


Below is the code example I used to ask to people:
 class A {  
   private int number;  
   int getNumber() {  
     return number;  
   }  
   void setNumber(int number) {  
     this.number = number;  
   }  
 }  
   
 public class App {  
   public static void main(String... arg) {  
     A obj = new A();  
     obj.setNumber(10);  
     function1(obj);  
     System.out.println(obj.getNumber());  
   }  
   
   private static void function1(A a) {  
     a.setNumber(12);  
     a = null;  
   }  
 }  
   


Try to guess the answer before scrolling down........






















7 out of 10 people told me that it will throw null pointer exception , which is incorrect answer.

Actual output:






Let me explain what is happening here:

in the main method First we have created object of type A and setting value 10 to that object.





After that we are calling function1 and passing "obj" as method argument, which is received by reference "a" .Below is the memory state:













After that we are setting value 12 to number via reference "a":














Than we are assigning null to reference "a" :



Here is the tricky part , After this the control will be returned to main method and we are using "obj" for printing value of number. "obj" is not null and contains modified value 12.

Key points to remember:
1) Java always pass method arguments by value only, yes you read it right, it is always always always always pass by value.
2) Java handles objects using their references we have created.
3) Two or more references can point to same object.


In this case the object reference value is copied and new reference is created for function1. But still it is pass by value only , never pass by reference. The object reference is copied not the actual object , as reference is the handle to access the object in java.

Why people trapped for pass by reference answer ??

Here you can see the value changed to 12 in function1 is visible in main method. This effect is the biggest reason why people think it is pass by reference. The value is changed because both the references pointing to same memory object in memory.


Now lets modify the function1 little bit and see what will happen:
 private static void function1(A a) {  
     a.setNumber(12);  
     a = null;  
     System.out.println(a.getNumber());  
   }  

Above program will give NullPointerException. Because the reference "a" is null now and we are trying to access null reference.



Next I am showing you similar example but this time it is for primitive type argument passing :
 public class App {  
   public static void main(String... arg) {  
     int a = 10;  
     function1(a);  
     System.out.println(a);  
   }  
   
   private static void function1(int b) {  
     b = 12;  
   }  
 }  

Here the outcome will be :






The reason here is also same  , as method argument is pass by value only, the value of "a" is copied to "b" only and here it is primitive type and they are having different memory location for both the variable, any change in "b" is not effective for "a".

One last exercise for you related to wrapper class:
 public class App {  
   public static void main(String... arg) {  
     Integer a = 10;  
     function1(a);  
     System.out.println(a);  
   }  
   
   private static void function1(Integer b) {  
     b = 12;  
   
   }  
 }  


That's it for now...

Please post your comments and doubts!!!