3 ms·
From: James Gosling Date: 1/13/11 Don Hopkins wrote: >Hey I know it's 2011 and we're living in the future and all that, but >there still seems to be s
by DonHopkins 12y ago
From: James Gosling
Date: 1/13/11
Don Hopkins wrote:
>Hey I know it's 2011 and we're living in the future and all that, but
>there still seems to be some confusion and debate about a matter I
>thought was perfectly clear: Is Java pass by value or pass by
>reference?
Depends on your terminology. But it's kinda both. It's pass-by-value for
everything, with the twist that for class instances, the thing that's
passed-by-value is the pointer to the object, so it behaves like
pass-by-reference.
From: Don Hopkins
Date: 1/13/11
Reminds me of the controversy about Niklaus Wirth's name:
"Whereas Europeans generally pronounce his name the right way
('Nick-louse Veert'), Americans invariably mangle it into 'Nickel's
Worth.' This is to say that Europeans call him by name, but Americans
call him by value." - Introduction by Adriaan van Wijngaarden at the
IFIP Congress (1965).
It seems to be a consequence of pretending pointers don't exist -- you
also have to pretend you have variables with object values and
references, but you still can't write a swap(a, b) function. If you
think of Java object values as pointers, then it's obviously passing
references by value. But if you don't believe in pointers and think of
variables as containing objects, then it's obviously passing objects
by reference, but your view of the world is an illusion, because it's
really implemented with pointers.
Or is it??? Someone suggested a Magic Elves theory, which helps a lot
of other stuff to make more sense.
Here's the discussion from 2011 about this issue that prompted me to ask James Gosling for a clarification. You can see that many people were VERY confused, and extremely fanatical about maintaining and spreading their confusion, in spite of the fact that it directly contradicted the Java spec and the inventor of Java himself.
Java is PASS BY VALUE.
http://www.theserverside.com/discussions/thread.tss?thread_id=61622 http://www.theserverside.com/discussions/thread.tss?thread_i...
- lucio 12y agoThe man says: >But it's kinda both. It's pass-by-value for everything, with the twist that for class instances, the thing that's passed-by-value is the pointer to the object, so it behaves like pass-by-reference. So he's saying: We call them all "chickens", but some of them walk like a duck and quack like a duck, so they're basically ducks. You can't extract from that quote "THEY ARE CHICKENS" (all in caps) When he says "kinda both" and "it behaves like pass-by-reference" I read some "subtle" hints saying that he thinks that basically class instances are passed-by-reference.
- jjnoakes 12y agoExcept they don't completely behave like they are passed by reference (see swap in the article). It is only ever correct to say "they behave like they are pointers which were passed by value". Continuing to say "they act like references" is wrong and how this mess keeps perpetuating.
- DonHopkins 12y ago"It's pass-by-value for everything" is a pretty clear, unambiguous definition that we can all agree with. I think he goes on to use weasel words like "kinda both" and "the twist" to mean that it's true in a more abstract sense, if you don't use the precise definition of "call by value", and you're fuzzy about exactly what you're referring to (conflating the object with a reference to the object), and vague about how many times you de-reference the pointer that you're passing as a value parameter. That's an unfortunate shortcut that many Java books and teachers take to teach Java to people who have no prior experience with any other programming language, to avoid teaching about pointers and other implementation details. That leads to a superficial understanding of the language, and a blind spot in their misunderstandings, which you can see on display in Mark Stock's, Mark Hedley's and Kevin Ryan's stubborn postings to this thread: http://www.theserverside.com/discussions/thread.tss?thread_id=61622 http://www.theserverside.com/discussions/thread.tss?thread_i... Java passes pointers to objects by value, which can be used to emulate some but not all aspects of passing objects by reference (i.e. swap). What your argument comes down to is that you prefer to think of the reference to the object you're passing by value as being the same thing as the object itself, which is not the case. The high level behavioral effect of passing a pointer to an object by value is that you are passing the object itself by reference. But it's impossible for a Java variable to actually contain an object itself, only a reference to an object, and there's no such thing as a reference to a variable or anything other than an object in Java. Parameter passing by reference requires being able to express a reference to a local variable of any type, including primitive types, not just an object, and you must be able to pass that reference to a function in a way that makes it possible to implement "swap(ref a, ref b)". Unlike Java, C# does have reference parameters, and it uses the "ref" keyword to distinguish them. Since normal C# parameters are passed exactly the same way as Java parameters, and C# has a "ref" feature that Java doesn't (you can implement swap in C# but not in Java), and you argue for calling Java parameters "pass by reference", then your argument applies to normal C# parameters too, so what do you call C# "ref" parameters if not references? Or do you carry your argument to its logical conclusion, and call both C# normal and "ref" parameters "references"? Conflating passing a reference to an object by value with passing an object or any other value by reference does not magically give you the power to implement swap, so it's not passing parameter variables by reference, it's only passing the objects that the objects that parameter variables point to by reference. The map is not the territory.