3 ms·
Java 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 mean
by jjnoakes 12y ago
Java 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?
- mannykannot 12y ago'If we can't discuss this topic using those well-defined things, what can we use?' I am told you will find all the things you need well-defined in the JLE, and in doing so you will avoid positing a plurality without necessity, as Billy Ockham may or may not have put it.
- jjnoakes 12y agoJLE? Or JLS? If you mean the spec (JLS) then I agree, you can find everything there. Including what I wrote above - that "Java object references" are the same thing as "Java pointers" (both in the JLS). The JLS also describes parameter passing in excruciating detail, and it matches the definition of "pass-by-value" (not "pass-by-reference") exactly. But if people in this thread could read and understand the JLS, this thread wouldn't exist. Since it does, some of us feel like we should contribute and correct the false statements we see.