Case 1: Pass by reference
Lets assume Java is pass-by-reference, so what should get printed on running below code:
Lets assume Java is pass-by-reference, so what should get printed on running below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class A { String name; public static void main(String[] args) { callingMethod(); } private static void callingMethod() { A myObj = new A(); myObj.setName("I am from calling method"); calledMethod(myObj); System.out.println(myObj.getName()); } private static void calledMethod(A myObj) { myObj = new A(); myObj.setName("I am from called method"); } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
It should be "I am from called method", right. But opposite to what we all thought, "I am from calling method" is the result, which gives an impression that Java is not pass by reference, but pass by value may be. Then lets test for pass by value nature.
Case 2: Pass by Value
Comment out the 17th line from the above code and see the result.
For pass by value, data in "myObj" in callingMethod should not get changed by changedMethod and "I am from calling method" should get printed. But instead, "I am from called method" is outputted this time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class A { String name; public static void main(String[] args) { callingMethod(); } private static void callingMethod() { A myObj = new A(); myObj.setName("I am from calling method"); calledMethod(myObj); System.out.println(myObj.getName()); } private static void calledMethod(A myObj) { // myObj = new A(); myObj.setName("I am from called method"); } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
For pass by value, data in "myObj" in callingMethod should not get changed by changedMethod and "I am from calling method" should get printed. But instead, "I am from called method" is outputted this time.
So what is Java then, pass-by-value? or pass-by-reference?
Answer is pass-by-value. Do remember this, Java is always PASS-BY-VALUE. So, lets see what happened in both cases when "myObj" was passed from callingMethod to calledMethod.
Initially, when new Object is created at line 10, memory is allocated for that object in heap space and "myObj" starts pointing to that memory. At line 11, "name" property in object memory is set to "I am from calling method".
At line 12, when "myObj" is passed to the calledMethod, coy of the reference "myObj" is created and that copy is passed to the calledMethod. However, it points to the same memory in heap space as "myObj". When name is changed in the calledMethod, it is changed in the shared memory between the 2 references in callingMethod and calledMethod.
See what happened exactly in both cases:
Case 1:
Case 2:
I have tried to make the diagrams self explanatory. Please post in comment section for any query related to the article.
I hope above explanation helps to understand why Java is pass-by-value and not pass-by-reference.
Join us at +Java Territory to stay updated with latest articles. Thanks for reading.
Initially, when new Object is created at line 10, memory is allocated for that object in heap space and "myObj" starts pointing to that memory. At line 11, "name" property in object memory is set to "I am from calling method".
At line 12, when "myObj" is passed to the calledMethod, coy of the reference "myObj" is created and that copy is passed to the calledMethod. However, it points to the same memory in heap space as "myObj". When name is changed in the calledMethod, it is changed in the shared memory between the 2 references in callingMethod and calledMethod.
See what happened exactly in both cases:
Case 1:
Case 2:
I have tried to make the diagrams self explanatory. Please post in comment section for any query related to the article.
I hope above explanation helps to understand why Java is pass-by-value and not pass-by-reference.
Join us at +Java Territory to stay updated with latest articles. Thanks for reading.
0 comments:
Post a Comment