12 ms·
> But Java isn't pass-by-reference. Yes it is. The thing that we talk about when we talk about "the value of foo", in Java, is passed by reference.
by lmm 9y ago
> But Java isn't pass-by-reference.
Yes it is. The thing that we talk about when we talk about "the value of foo", in Java, is passed by reference.
- lisper 9y ago> > But Java isn't pass-by-reference. > Yes it is. And this is my point in a nutshell. This argument will never be resolved, and so continuing to argue over it isn't helpful no matter how hard you pound your fist on the table.
- colanderman 9y agoThe argument is resolved pretty simply by considering that the term "pass-by-reference" was defined well before Java even existed, and that Java does not exhibit behavior required of that calling style. (In Java, you cannot change a variable in the caller's scope to point to a different object.) Java is demonstrably not pass-by-reference. But don't take my word for it. Pass-by-reference was understood by the person who invented the calling style used in Java (Barbara Liskov) not to apply to that calling style. [1] The argument was resolved before it could even be had, 44 years ago. [1] https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sh...
- esrauch 9y agoJava is also demonstratably not pass-by-value (for objects). It is a false dichotomy to prove it is one by proving it is not the other.
- mbrock 9y agoYou might think that the value of foo is that which is compared when evaluating foo == bar, which is a pointer!
- lmm 9y ago> You might think that the value of foo is that which is compared when evaluating foo == bar Few if any working Java programmers do think like that though. Like, if you have String x and String y, and println(x); prints "foo" and println(y) prints "foo" and x != y (a perfectly ordinary situation to get into in Java), we would still say "the value of x is "foo"".
- mbrock 9y agoEquality on strings is a common source of confusion in Java, and I think that's because many newbies aren't properly taught the kind of semantic pedantry we're discussing... and because the language design is weird. Strings pretend to be immutable value objects, but they still have object identity, which doesn't really make sense as far as I can tell. It still seems to me that the only good definition of an object variable's "value" in Java is its pointer.
- lmm 9y ago> Strings pretend to be immutable value objects, but they still have object identity, which doesn't really make sense as far as I can tell. Yeah. Java doesn't have proper value objects, there's these vestigal referencey effects everywhere, probably because of the language being designed for much more memory-constrained environments than those where it's actually used today. Still, most of the time you can just ignore that, remember a few simple rules like "always use .equals rather than ==" and you're good. > It still seems to me that the only good definition of an object variable's "value" in Java is its pointer. Good for technical pedantry maybe. Good for working programming, no - 99.9% of the time when you're talking about the "value" (not in terms of function-passing semantics, just generally) you want to talk about "foo", not about "an object reference to address 54fc892d".
- mbrock 9y agoTechnical pedantry could almost be a definition of programming...
- 9y ago
- colanderman 9y agoNo, it is not. Prove it to me by writing a Java function which can cause a variable in the caller's scope to point to a different object. You cannot, because Java is not pass-by-reference. C++ is, and you can do this. So are macros in many languages, and you can do this. Java is not pass-by-reference.
- lmm 9y ago> Prove it to me by writing a Java function which can cause a variable in the caller's scope to point to a different object. That's not what the term means and you know it. The difference between passing by reference and passing by value is that in the plain meaning of the words (all terms eventually come to mean their plain meanings, because language is defined by consensus): whether the called function receives a value, or a reference to the value in the caller's scope. Java is the latter (for non-primitives); you can tell because the called function can modify the value in the caller's scope. If you want a term to mean "allows changing the binding in the caller's scope", coin one, but pick words whose plain meaning matches the meaning you want.
- colanderman 9y agoNo. Pass-by-reference has meant exactly what I described, for at least 44 years, when Barbara Liskov decided it didn't apply to the calling scheme she invented, which was later inherited by Java. Read: https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sh... Specifically, "it is not call by reference because access is not given to the variables of the caller, but merely to certain objects."
- lmm 9y agoCoining a new term is the right approach if you want to draw that distinction. "Java is call by sharing" would be fine. "Java is call by value" is more wrong than "Java is call by reference".
- 9y ago