static void f(Foo x) {
x = ...;
}
// somewhere
Foo y = ...;
f(y);
The pointer value of y cannot be changed from inside f. Therefore Java is pass-by-value.Java also has restricted semantics on pointers, but that has nothing to do with the calling convention.
(Edit: Note that the object which y/x points to can be changed, but that's not the question.)
void f(const Foo& x){
...
}
My point is the semantics don't map 1:1. If you're going to say Java's references are pointers with different semantics how is that semantically different from saying Java's references are references with slightly different semantics? void f(const Foo&_x){
Foo& x=const_cast<Foo&> (_x);
…
}
What do you think is going on here?I think you're adding code that doesn't address my point. This is pointer manipulation, not function call semantics. If you have raw pointers you can do a lot of magic. I think your code actually demonstrates how not-pointer like Java's references actually are.
void f(Foo* x) { Foo y; *x = y; }
void f(Foo& x) { Foo y; x = y; }
void f(const Foo* x) { const Foo y; x = &y; }
void f(const Foo& x) { const Foo y; ?; } void f(Foo* x) { Foo* y = new Foo(); x = y; } // C++
public void f(Foo x) { Foo y = new Foo(); x = y; } // Java
If 'x' was a reference then you couldn't do that.The rest of it (const, const_cast, arrays-are-kinda-pointers in C++, taking the address of stack variables, etc.) are just weird quirks in the language semantics. (Honestly, C++ is already a quirky enough language on its own to make detailed comparisons inherently problematic.)
At the most fundamental level, C++ supports both call-by-value and call-by-reference, while Java only supports call-by-value.
I'm just saying philosophically if you're going to say it is pass-by-pointer w/ castrated pointers is that any different than pass-by-reference with castrated references?
To blur the lines: If escape analysis is enabled its entirely possible for an object to allocated on the stack. In this situation a callee could manipulate the parent stack. Granted it can't overwrite the entire region wholesale, but you could functionally overwrite it all, but java doesn't have any way of doing that semantically anyway.
Java object references are mostly like C++ pointers. Even though you can't do arithmetic on them, the way they behave during assignment and parameter passing is identical. The primary qualities of a pointer are that it points to something, copies of it are shallow (and cheap) and point to the same thing, equality of pointers implies equality of the objects (but not vice versa), etc. All the same in both languages.
Nothing in Java is like C++ references (except the word reference) because the primary qualities of references are that they alias the objects that they refer to, they can not be rebound, and operations on them act exactly the same as operations on the aliased objects, with no dereferencing operators necessary, including assignment.
Once you understand that, you can begin to grasp how Java can be fully pass-by-value and yet your function can modify a list and the caller will see the change.