- Pass by value, values hold object references. (java, c# objects)
- Pass by value, values hold objects. (c# structs)
- Pass by reference, values hold object references. (c# inout parameter, passing object)
- Pass by reference, values hold objects. (c# inout parameter, passing struct)
In general, the whole thing about passing a reference by value is just BS. What you need to ask is, "What am I passing to the function?"
If you are passing an object, and Java (or whatever language you are using) determines a reference (memory address or whatever), then passes that reference to the function, then you are, by definition, passing an object by reference. The important thing to focus on in this argument is, "What am I passing?" The answer for this scenario is that the programmer is trying to pass the object itself. Who cares about the address / location of the object.
If you decide to pass the reference explicitly, then you are passing a reference, not the object. This would be like explicitly passing a pointer in c++, and is apparently a controversial topic when it comes to Java. A reference will nearly always (I can't actually think of a case where it isn't) be a primitive, and therefore, be passed by value.
For argument sake, say I'm wrong about all this. My question would be, "What would it look like to pass an object by reference, and how does that differ from what is currently happening?"
To pass an object by reference, it looks exactly like C#'s out/ref parameter.
void function(ref SomeClass arg) {
arg=null;
}
var a=new SomeClass();
function(a);
// now a is null
Which is different from what happens without ref. (or by default in Java)The spec also says
> there must be a standard way for the compiler of the creation expression to pass a reference ... to the member class's constructor
So it seems perfectly correct to say that "objects are passed by reference", and that those references could also be referred to as values.
That last sentence is a bit backwards compared to the quote. The references are passed by value if one must put it like that. But nothing gets passed by reference in Java.