|
|
|
|
|
by twic
794 days ago
|
|
No, Java really is pass by value. You can rely on this in Java: String s = "hello";
foo(s);
assert s == "hello";
In C++, if the function takes a non-const reference, you can't.Yes, Java always passes pointers to objects. But you can pass pointers by value. And passing a pointer by value is not the same as passing by reference! I think the origin of the confusion around a function taking a list by value and the like is the implicitness of pointers in Java and its cousins. This Java method: void foo(List<String> strings)
Is the equivalent of this C++ method: void foo(shared_ptr<List<string>> strings)
True systems languages make the pointers explicit. |
|
In C++ passing a pointer by value is effectively the same as passing a reference, the only real difference being that the syntax for accessing the underlying value is more implicit for a reference.