Hacker News new | past | comments | ask | show | jobs | submit login

I understood that in Java, objects are passed by reference and scalars by value?



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.


I 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.


It's a technicality. You always get a copy of what you pass in. When you pass a primitive, eg an int that has the value 7, you get a copy of that value 7. When you pass an object, you get a copy of the reference to that object.

You understood correctly, it's just how it gets phrased.


That's true. I think this article is being a bit pedantic on this point.

You can never have a variable that directly represents an object in Java. You can only ever hold a reference to an object. Those references to objects are passed by value.

So his statement is more technically correct, but in general "objects are passed by reference and scalars by value" is the understanding that an interviewer would likely be looking for.


The distinction between “pass by reference” and “pass by value” started with Algol, if I recall correctly, and made sense in that language, where you can declare which arguments of your function are passed in which way. It also makes sense in C++, which has a similar feature.

For other languages, I think framing parameter-passing in these terms just creates more confusion then it resolves. I would rather describe the situation like this: In Java, when you pass a mutable object (or array) to a method and the callee mutates it, the caller will see the changed version.


Correct. The reference itself is passed by value, though, so assigning a new object to that variable would not affect the caller.

Most of these "wrong" answers are only guilty of not being explicit enough to be a page long.


pass-by-reference has a particular meaning. Java does not support this.

There are plenty of pages on the web explaining why suggesting Java support pass-by-reference is incorrect.

http://www.google.com/search?q=java+pass+by+reference


No, everything is passed by value, it just happens that objects live in a reference model.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: