3 ms·
If 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'
by lmm 9y ago
If 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.