4 ms·
Yeah 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
by nickbauman 12y ago
Yeah 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.
- nickbauman 12y agoDude. I've been using Java since 1.0. You're talking about Java from the perspective of C++, which is confusing to someone who is actually programming in the Java language.
- jjnoakes 12y agoRead the JLS. This is Java as defined by its creators, absent of C++.
- mannykannot 12y ago"The issue is that you don't understand that the value of a Java object reference is the same as a C++ pointer" A C++ pointer is a simple thing: an address within the program's address space. In an earlier response to a similar claim of yours, RayleyField pointed out that "In HotSpot references are double pointers (useful for moving GC), but in general it's up to JVM implementation." Certainly, if you dig into a Java reference, you will find an address that is analogous to a C++ pointer, but the same can be said of a C++ reference. Java references are not exactly equivalent to either C++ references or C++ pointers, and the statement that Java passes by value is not dependent on an arguable claim that Java references are more like C++ pointers than C++ references. This line of argument has just added to the confusion in these threads.
- jjnoakes 12y agoWe are talking logically, not implementation details. Logically, a C++ pointer and a Java object reference are nearly identical, as described and defined by their respective language standards. Independent of the implementation chosen. This is not the same as a C++ reference, which is logically an alias (and may be implemented via pointers, but that's irrelevant). The confusion in this thread stems from people confusing many things, indeed.
- mannykannot 12y agoIf "we" are talking logically, then "we" should not be using the expression "C++ pointer" in the statement I quoted earlier. Doing so can only lead to confusion in the minds of people struggling to understand Java argument-passing.
- jjnoakes 12y agoWhy not? A C++ pointer is a logical construct that exists in the language (as per the language standard) independent of any given implementation. Same with "Java object reference" and "Java pointer" (synonyms). If we can't discuss this topic using those well-defined things, what can we use?
- barrkel 12y agoJava objects are not value types; they're reference types. You're confusing your concepts. Specifically, you're conflating parameter passing semantics with reference type semantics. The problem with this conflation is that reference type semantics cover more situations than merely parameter passing, so when you conflate the two concepts, you miss out on the remaining bits. Consider this: Foo x = new Foo(); x.setBar(42); Foo y = x; y.setBar(0); assert(x.getBar() == 42); // fails! astonishing behaviour! If the value of x was assigned to y, surely modifying y shouldn't affect x, right? That's what assignment does, it takes the value on the right and puts it in the location on the left, correct? So why on earth was x modified when we only called a mutator method on y? To understand this, you need to know that Java objects are reference types. And this has nothing to do with parameter passing. And once you understand reference type semantics, you don't need anything extra to understand parameter passing, because parameter passing in Java uses the same semantics as assignment: that is, the value of the actual parameter is copied into the formal parameter, just like values on the right of an assignment are copied into the locations on the left. It's pass by copy.