Better HN
topnewbestaskshowjobs
Log inSign up

agricola

1 karma · joined Mar 22, 2015 (11y ago) · 3 submissions

Submissions

comment 11y ago · link · parent

I'm not really sure what point you are trying to make here.

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?"

comment 11y ago · link · parent

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.