4 ms·
Everything in Java is passed by value, as is the case in, for example, C[1] and Ruby. That the value that is passed is also called a reference does not change t
by Confusion 15y ago
Everything in Java is passed by value, as is the case in, for example, C[1] and Ruby. That the value that is passed is also called a reference does not change the fact that it is not call-by-reference.
There is a simple way to check whether a language does call-by-reference: can you write a swap function such that after calling swap(a, b), the variables a and b in the calling function refer to different values than before? In Java this is impossible.
Because the terminology is so confusing when object reference values are being passed-by-value, Barbara Liskov suggested we call it call-by-sharing.
[1] you can make C look like it's doing pass-by-reference by throwing some & and * operators around. That doesn't change the fact that it is purely call-by-value.
- JoeAltmaier 15y agoI believe any object passed in Java can be modifed by the receiving method, right? So that is pass-by-reference. If we want to say we're really passing a reference by value, that begins to be silly - if the object passed can be modified, its a reference.