News for hackers. Stuff that matters.
Re:Java is Pass-by-Value, Dammit by barrkel on Sunday March 22, 2015 @09:31AM (#9246675)
If 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.

Re:Java is Pass-by-Value, Dammit by nickbauman on Sunday March 22, 2015 @01:44PM (#9247600)
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.

Re:Java is Pass-by-Value, Dammit by barrkel on Sunday March 22, 2015 @09:28PM (#9249014)
Java 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.

Re:Java is Pass-by-Value, Dammit by jjnoakes on Sunday March 22, 2015 @06:31PM (#9248536)
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.

Re:Java is Pass-by-Value, Dammit by mannykannot on Monday March 23, 2015 @08:27AM (#9250484)
"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.

Re:Java is Pass-by-Value, Dammit by jjnoakes on Monday March 23, 2015 @09:46AM (#9250816)
We 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.

Re:Java is Pass-by-Value, Dammit by mannykannot on Monday March 23, 2015 @12:10PM (#9251623)
If "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.
Re:Java is Pass-by-Value, Dammit by jjnoakes on Monday March 23, 2015 @12:47PM (#9251831)
Why 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?

Re:Java is Pass-by-Value, Dammit by mannykannot on Monday March 23, 2015 @05:47PM (#9253539)
'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.

Re:Java is Pass-by-Value, Dammit by jjnoakes on Monday March 23, 2015 @06:34PM (#9253755)
JLE? 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.

Re:Java is Pass-by-Value, Dammit by mannykannot on Tuesday March 24, 2015 @08:01AM (#9256033)
You are right, JLS. With regard to whether these comparisons with C++ have led to the desired results, see elsewhere.
Re:Java is Pass-by-Value, Dammit by mannykannot on Tuesday March 24, 2015 @07:52AM (#9255994)
The well-definedness of 'C++ pointer' is a non sequitur - it is not a sufficient condition to justify its use in this context. 'Backwardation' is well-defined, yet I doubt it can contribute anything to this discussion.
Re:Java is Pass-by-Value, Dammit by jjnoakes on Wednesday March 25, 2015 @08:57AM (#9262572)
If you consider the context of my statement, and not my statement as it stands in isolation, then you are mistaken.
Re:Java is Pass-by-Value, Dammit by nickbauman on Sunday March 22, 2015 @09:24PM (#9248999)
Dude. 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.
Re:Java is Pass-by-Value, Dammit by jjnoakes on Sunday March 22, 2015 @10:23PM (#9249172)
Read the JLS. This is Java as defined by its creators, absent of C++.