12 ms·
I think the confusion was largely created by the author. When he asserts that nothing in Java is passed by reference, he's using "reference" in the C++ sense. T
by dlubarov 12y ago
I think the confusion was largely created by the author. When he asserts that nothing in Java is passed by reference, he's using "reference" in the C++ sense. Then he says things like "references are passed by value", which uses "reference" in the Java sense.
We should be consistent in terminology. If we accept Sun's terminology, then objects are indeed passed by reference. If we're insisting on the more traditional C++ terminology, then Java has no references, only pointers.
The author recognizes the two meanings of "reference", but isn't sufficiently clear about which meaning he's using at a given time.
- ajanuary 12y agoIf we're using the "reference type" meaning of reference, then the phrase "objects are passed by reference" is grammatical nonsense. At best it becomes a tautology - "reference types are passed as reference types". What does that even mean? That reference types don't magically become value types when you pass them in as arguments? That doesn't actually say anything useful about the evaluation strategy.
- dlubarov 12y agoIt would mean that the invoked method gets a reference to the original object rather than its own copy of the object. It's only obvious if we assume some knowledge of Java's semantics.
- mannykannot 12y agoYou appear to be saying 'we can call it pass-by-reference if we use a special definition of the term when speaking about Java.' "A word means what I want it to mean, nothing more, nothing less." - Humpty Dumpty, 'Through The Looking Glass and What Alice Found There'. There should be a variant of Occam's Razor specifically against unnecessary semantic splitting and proliferation.
- barrkel 12y agoIf you already know Java, you have no need to have Java's parameter passing mechanism explained to you. If you don't know Java, and need an explanation, then you need one based on terminology that exists outside Java. Pass by value and pass by reference are concepts that predate and exist outside of Java, describing programming language semantics in a way where statements about different languages can be compared and contrasted, because they're not phrased in vocabulary specific to the language the statement is about. Java passes all arguments by value. The idea of overloading the term 'reference' to mean 'pointer', but without all the power you get from pointers in languages like C, has at this point grown far beyond Java. I don't think it's a logical muddying of the waters to use it in a phrase like "references are passed by value" when describing Java using terminology that exists outside of Java.
- nickbauman 12y agoYeah I don't agree on two fronts: 1) To say that Java "passes all arguments by value" implies astonishing behavior if one passes an object and calls a mutator on that object changes it everywhere else that reference is used. If it were truly passed by value, the target would get a _copy_ of the original. Which is NOT the case in Java. So this terminology is misleading! 2) An admittedly weaker argument you imply that a given Java developer has never looked at any other language that doesn't use Java's approach to parameter passing. That's a tiny, tiny audience, my friend.
- jjnoakes 12y agoJava does pass all arguments by value. The issue is that you don't understand that the value of a Java object reference is the same as a C++ pointer, which means passing it by value into a function means simply that you've copied the pointer, nor the object, and mutating that object inside the function is simply following the pointer copy to the same pointed-to object, so the caller also sees the change. Note that it you change the pointer (object reference) directly, instead of following it and mutating the pointed-to object, the caller would NOT see the change. Like "x = new Foo()" inside a routine would not change what was passed in at all. But if Java was pass by reference, then things would act differently. Please learn Java. It's not about not knowing other languages. It's about not knowing Java. This is all in the JLS.
- mannykannot 12y agoIt is not the author causing confusion here between the term 'reference' in C++ and Java. For the purposes of this discussion, there is no difference.
- jjnoakes 12y agoThere are differences. References in C++ are aliases. References in Java are pointers. C++ lets you pass-by-reference (meaning aliasing) or pass-by-value (including objects, pointers, ints, etc). Java only lets you pass-by-value (object references, ints, etc. There are no object values in Java, so those can't be passed at all). Correctness is important.
- mannykannot 12y agoCorrectness is indeed important, which is why I wrote 'for the purposes of this discussion.' The topic of the original article is what Java does, and additional capabilities in C++ are not pertinent. Dlubarov posited that the author has sown confusion by using the term 'reference' in a C++-specific way, but I believe that confusion arises from a misreading of the article.
- jjnoakes 12y agoThe phrases "pass-by-reference" and "object reference" need to be (and are) well-defined for the purposes of this discussion and for Java as it stands alone separate from C++. But also, for the purposes of this discussion, it is very useful and illustrative to equate pass-by-reference to C++ references, and object references to C++ pointers, because the languages are similar and the concepts match up very well, and because these things are more explicit in C++. Sure, one could have defined everything without comparing to C++ (in fact that is what the JLS does) but, for the purposes of this discussion, many of us assumed that wasn't good enough for many posters here, because if it was, this discussion wouldn't exist.
- mannykannot 12y agoYou are, of course, entitled to your opinion that the best way to clear this up is by making comparisons to C++, but the problem is that in defending your comparisons, you have brought in various issues, such as that C++ references cannot be rebound and the differences between the meaning of 'pointer' in the two contexts, that can be dispensed with if you do not take this approach. Personally, I think that those people who are both confused and looking at this from a C++ perspective would be better advised to stop making comparisons and look at Java for what it is.