4 ms·
I think people are reading way too much into what a reference is. In c++, it has a very specific meaning, but in Java it has a slightly different definition.
by agricola 12y ago
I think people are reading way too much into what a reference is. In c++, it has a very specific meaning, but in Java it has a slightly different definition.
In c++, the only difference between a reference and a pointer is in the dereference syntax, and that a pointer can be reassigned. In the background, the variable is a pointer in both cases. It is simply syntactic sugar.
Another thing some people are mixing up is the entire philosophy between pass-by-reference and pass-by-value. The ability to write a simple swap function does not accurately demonstrate what a language is doing. It is quite simply whether or not the compiler creates a copy of the object (not a copy of the address / pointer / reference). If, in the background, Java passes an address / pointer / reference (the word makes absolutely no difference), when the programmer is attempting to pass an object, that is the definition of pass-by-reference regardless what arbitrary word I want to name the referencing value. Yes, the pointer is a copy of the previous pointer, but that's not the explicit value that the programmer cares about.
Yet another comparison to c++: the same thing happens with pass-by-reference in c++. If a function has the prototype:
void foo(Dog& d);
and I pass a Dog object in a call similar to:
Dog d("Spot");
foo(d);
then, in the background, it is going create a reference (i.e. the address), and copy it on the stack. In my foo() function, the argument is simply already dereferenced (I don't have to treat it like a pointer).
TLDR...
If you pass an object to a function, and operations performed by that function affect the original object, it is pass-by-reference. Period.