2 ms·
There's a lot of confusion in this thread about what "call-by-reference" means. Let's be very clear. If Java were "call-by-reference", then this function:
by colanderman 9y ago
There's a lot of confusion in this thread about what "call-by-reference" means. Let's be very clear. If Java were "call-by-reference", then this function:
static void foo(Object x) {
x = null;
}
would have an effect visible to the caller. This is exactly the case with C++ references, and C and Lisp macros, which are call-by-reference. (C++, C, Lisp, and the term "call-by-reference" all having existed prior to Java, by the way, so Java and its users do not get to redefine it.)
But the above function does nothing in Java. Hence, Java is not call-by-reference.
Wikipedia has a nice quote [1] from Barbara Liskov (the namesake of the Liskov substitution principle), made when she invented the passing style used in Java in 1974, for use in her language CLU:
"In particular it is not call by value because mutations of arguments performed by the called routine will be visible to the caller. And it is not call by reference because access is not given to the variables of the caller, but merely to certain objects."
She settled on the term "call-by-sharing". Maybe we should all just agree on that.
[1] https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sh...