4 ms·
I always just call this sort of thing "pass-by-pointer-value" to clear up any confusion. To someone coming from C++, where objects are actually passed by value,
by colanderman 9y ago
I always just call this sort of thing "pass-by-pointer-value" to clear up any confusion. To someone coming from C++, where objects are actually passed by value, saying that Java (or Python, or what-have-you) does the same is just confusing.
- andrewstuart2 9y agoAnother way of saying this is "reference by value." The people who say things are pass-by-reference when they're "pass by pointer value" have likely never worked with a true pass-by-reference language, and are just taking the phrase at its English face value.
- chi3 9y agoComing from C++, if "passing by pointer value" is considered "passing by value", then what is the opposite? Not passing at all? I mean, there's always going to be _some_ kind of value being passed?
- lmm 9y agoIf you write foo(&a), both "you pass a by reference" and "you pass &a by value" are fair descriptions of what happens. The confusion arises because Java doesn't explicitly distinguish between a and &a; if you write "Foo a = new Foo();" in Java, the behaviour is similar to "Foo *a = new Foo();" in C++. So if we ask how "a" is passed, is the "a" we're talking about the reference or the value?
- chi3 9y agoAh, I see the reasoning. I only did basic Java in university so I might be remembering incorrectly, but aren't variables storing non-primitive values called "References"? Seems weird to me to say that Java "passes by value" when all objects being passed are actually references to objects.
- lmm 9y ago> I only did basic Java in university so I might be remembering incorrectly, but aren't variables storing non-primitive values called "References"? Technically yes, but since the language doesn't let you talk about references directly, the concept rarely comes up. Like, in theory you could say "a is a reference that refers to a value of type Foo", but people don't actually say that; they just say "a is of type Foo". > Seems weird to me to say that Java "passes by value" when all objects being passed are actually references to objects. Indeed, completely agreed.
- emidln 9y agoYou do see this kind of language in Clojure, where you can talk about a var separately from its value.
- zamalek 9y agoThe pointer value is passed (which is a valid contention), but that is what it has been defined as for decades.
- TheCoelacanth 9y agoIn C++, terms void f(Foo x); // pass-by-value void f(Foo* x); // pass-pointer-by-value void f(Foo& x); // pass-by-reference In the last example, you aren't passing in a value, you are essentially creating an alias for some variable in the calling scope.
- chi3 9y agoBut it's still just a pointer underneath? It's more or less just syntactical sugar which makes it dereferenced it for you, and I'd argue that it only makes sense to use the phrase in certain contexts. The only real difference between the last two is that the last one shouldn't be sent in as null, isn't it? The test in OPs artictle doesn't really make sense either, because you can switch the values using pointers as well.
- colanderman 9y agoMy made-up term "pass-by-pointer-value" just emphasizes the fact that in Java, Python, and sundry other OOP languages, you are always passing around a pointer to an object, even if it is implicit. This is why e.g. equality usually checks object identity (pointer value) in these languages (Python being a notable exception). C++ just forces you to be explicit about this, as others have pointed out.
- deleted 9y ago[deleted]
- cup-of-tea 9y agoIt makes a whole lot more sense in C++ too because you can actually pass by reference.
- colanderman 9y agoYes. This is what lmm and others in this thread are missing. Pass-by-reference includes behavior which Java does not exhibit but which several other languages (C++, C and Lisp macros, etc.) do. You can't call Java pass-by-reference just because it feels right to you. It is a phrase which means something which some languages are and Java is not.
- deleted 9y ago[deleted]
- Terr_ 9y agoI like to lead people into it with: "Pass boolean by value" "Pass integer by value" "Pass reference-to-T by value."