30 ms·
The article says almost exactly that: "However, Objects are not passed by reference. A correct statement would be Object references are passed by value."
by nbouscal 12y ago
The article says almost exactly that:
"However, Objects are not passed by reference. A correct statement would be Object references are passed by value."
- revelation 12y agoYeah, it says that in one sentence while its the central point of confusion if you even want to call it that. Instead it rants at length to somehow save definitions that are clearly not particularly helpful in a language like Java. This article just reeks of this certain kind of socially inept, completely unhelpful attitude and technical pedantry that has infected mailinglists and IRC channels over the world. Actively trying to misunderstand people in an effort to expose their ignorance or unfamiliarity with intricacies of whatever language or tool they are trying to navigate. Its a terrible habit up there with feigned surprise.
- inflagranti 12y agoThis is a very unjustified and overly harsh criticism. The article clearly presents real world confusion that can arise from not understanding that Java is not pass by reference. This can easily lead to wrong code. So it's not just pedantically insisting on some specific terminology, but highlighting the semantically differences between passing a reference (more precisely pointer) by value vs. 'real' pass by reference.
- olavk 12y agoI've never seen actual bugs due to a misunderstanding of the concept though. It seem to be mostly a terminology difference between c++ and Java cultures. It is correct to say that Java passes objects as references, but it is technically wrong to say that Java is pass-by-reference. But since Java has only one way of passing objects, this terminology confusion does not lead to actual problems.
- jjnoakes 12y agoExcept they are different things, and others (me included) have seen bugs related to the confusion. Technical correctness is always best. Java is pass by value, and even variables holding object references are passed by value.
- dlubarov 12y agoI 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.
- swatow 12y agoI agree it's bad habit, but I don't see it in the article. The article goes to great length to argue why Java should use the same terminology as other languages. I would not say that the statements: Sun wanted to push Java as a secure language, and one of Java's advantages was that it does not allow pointer arithmetic as C++ does. They went so far as to try a different name for the concept, formally calling them "references". A big mistake and it's caused even more confusion in the process. can be called "actively trying to misunderstand people". In fact it shows an understanding with the position he's arguing against.
- DonHopkins 12y agoOne amusing artifact of that leaky abstraction is "java.lang.NullPointerException". How is that even possible, if Java doesn't have pointers under the hood?
- Tobani 12y agoThere are no claims that Java doesn't use pointers. You just don't have direct access to them. Also i don't. Think the entire purpose was to hide pointers for correctness sake but to make the memory model consistent across plarforms.
- DonHopkins 12y agoYou don't have to "actively try to misunderstand people" who confuse the meaning of the word "as" with the meaning of the word "by". They are confused about the precisely defined, widely understood meaning of words, and they already actively misunderstand the English language, as well as the Java language specification. If I drove your grandmother BY a car, it would be totally different than driving her AS a car (logically: if your grandmother had wheels, then she'd be a car). In the same sense, passing the value of an object BY reference is totally different than passing a reference to an object AS a value. Nobody's "actively trying to misunderstand people," because those people are already actively confused and insist on clinging to and spreading their shallow, incorrect definitions that directly contradict the Java language specification itself. No matter how many times how many people patiently explain it to them in clear unambiguous terms, they still insist they're right and everyone else including the Java spec and James Gosling himself are wrong. Just look at all the ignorant postings in this thread by people like Mark Stock, Mark Hedley and Kevin Ryan, who just can't get it through their heads that they're wrong. http://www.theserverside.com/discussions/thread.tss?thread_id=61622 http://www.theserverside.com/discussions/thread.tss?thread_i...
- jjnoakes 12y agoAlso up there with taking a technically correct article personally and flaming the author instead of fixing your own misunderstandings.