OK, I get your point : the reference is passed by value :-)
But still what's passed is a reference... don't you agree ?
Of course :) You pass *a* reference, but you don't pass *by* reference. That's a subtle but important difference.
Can you do it another way in a JVM anyway (you cannot "dereference a pointer" like in C to get its value, everything in Java is referenced AFAIK - excepted primitives) ?
Good observation Remy, indeed you can't. There's no concept of a "value" or "stack" object in Java like languages as C++ or C# do have. For anyone having a C++ background the difference is rather obvious. Syntax wise Java references may look like C++ stack objects, but they are actually just pointers. In C++ it's immediately clear what's the difference between passing a stack object or passing a pointer and doing any of that either by-value or by-reference (since there's explicit syntax for that).
So, there are atleast 4 possibilities of passing a parameter:
Stack object by value (entire object is copied and passed)
Pointer by value (pointer is copied and passed)
Stack object by reference ( address of variable holding stack object is passed)
Pointer by reference ( address of variable holder pointer is passed)
Each of these has its own behaviour.
In Java there's only 1 possibilty (pointers being passed by value), so a programmer who has only programmed in Java may not really be aware of or focussed on these differences.
We all agree here that there are no stack objects in Java (suprizingly, sometimes people even try to argue with that, saying Java doesn't have pointers so it must have only stack objects). This leaves us with only two options left. As I've been saying before, they key difference between pass-by-value and pass-by-reference is that one can change what the original variable holds (not change the thing it points to, but change *what* it points to). You can't do that in Java, so therefore you don't have call-by-reference semantics ;)
It does not really matter, it's just brain masturbation, but I'd be interested in some official material that details what you say, if you have pointers.
Well, for starters you might just want to read "the dragon book" (
http://www.amazon.com/gp/product/0201100886/104-5987390-6021516?v=glance&n=283155 ). It has lots of details on the various passing mechanisms (more or less language neutral). I've been reading this when I first started building compilers and it greatly enhanced my insight.
More specifically I would like to refer to Sierra & Bates in their SCJP book. It has an entire section devoted to exactly this topic:
I quote:
Does Java use pass-by-value semantics?
If java passes objects by passing the reference variable instead, does that mean Java uses pass-by-reference for object? Not exactly, although you'll often hear and read that it does. Java is actually pass-by-value for all variables running within a single VM. [...]
It makes no difference if you're passing primitive or reference variables, you are always passing a copy of the bits in the variable. [...] if you're passing an object reference variable, you're passing a copy of the bits representing the reference to an object.
This is near official Sun material as Kathy and Bert are among the people who actually create the SCJP exams.
(ps also notice *single VM* being mentioned. When doing RMI, Java can do something like "pass-by-deep-copy" for the special 'remote' objects)