Passing by What?
Is it by value or it by reference?
Let's see how we can go about understanding this. Java uses references to point to objects. So, when I call a method which has an argument of an Object A, what we are sending is a copy of the reference we created into the method. Oh! that means we are sending the argument by value. Really? whether it is the reference outside the method or inside, both point to the same object so, both are actually references to the same object and any change in the attributes of the target object would reflect back.
Simply put, if java was passing an object by value, then we would not be seeing changes. But hey! java does not even pass objects, it passes references. These references are obviously passed by value. Why, you ask? Well , let's make a reference to an object and call a method with this reference as an argument and inside the method we point to another object. There would be no changes in the original reference, although the one inside the method would now point to a new object.
So, java has references to objects which are passed by value, but the references themselves both point to the same object.
So, to conclude why people think java is pass by value is because we pass references a copy of which is passed to the method, so Aha! it is pass by value. But then, I ask arent both references referring to the same object in which case we are sending a link to the object by reference.
My take : it is all perspective. with respect to a reference, it is pass by value, with respect to the actual object, it is pass by reference.
Java passes references by value.
Also, while we are at this, it is worthwhile to notice that primitive objects are always passed by value.
Bonus Example: send an array as an argument and edit stuff, you will see a change in original array.
Is it by value or it by reference?
Let's see how we can go about understanding this. Java uses references to point to objects. So, when I call a method which has an argument of an Object A, what we are sending is a copy of the reference we created into the method. Oh! that means we are sending the argument by value. Really? whether it is the reference outside the method or inside, both point to the same object so, both are actually references to the same object and any change in the attributes of the target object would reflect back.
Simply put, if java was passing an object by value, then we would not be seeing changes. But hey! java does not even pass objects, it passes references. These references are obviously passed by value. Why, you ask? Well , let's make a reference to an object and call a method with this reference as an argument and inside the method we point to another object. There would be no changes in the original reference, although the one inside the method would now point to a new object.
So, java has references to objects which are passed by value, but the references themselves both point to the same object.
So, to conclude why people think java is pass by value is because we pass references a copy of which is passed to the method, so Aha! it is pass by value. But then, I ask arent both references referring to the same object in which case we are sending a link to the object by reference.
My take : it is all perspective. with respect to a reference, it is pass by value, with respect to the actual object, it is pass by reference.
Java passes references by value.
Also, while we are at this, it is worthwhile to notice that primitive objects are always passed by value.
Bonus Example: send an array as an argument and edit stuff, you will see a change in original array.
2 comments:
I give this awesome post a pass !! phew !!! wish I knew Java :)
Thanks for making a stop while passing by. You have quite an interesting cartoon blog.
Post a Comment