By Fusion
via nexus.zteo.com
Published: Feb 03 2008 / 16:35
Contrary to what is announced by a recent blog entry making DZone's front page, Java does pass primitives by value and objects by reference. This entry replies to the original entry and follows it in "C Country" to remove all confusion.
Comments
Daniel Dyer replied ago:
Java passes references by value. You can't write a swap function in Java, so it is not "pass-by-reference" in the convential interpretation of the term.
http://javadude.com/articles/passbyvalue.htm
markturansky replied ago:
All objects are instantiated and live on the heap. References are passed around.
,
ethzero replied ago:
That's not what pass-by-reference means. A reference is a kind of value. When you pass-by-reference you're passing a pointer to a value on the stack. Whether that value is a primitive or reference to an object on the heap is irrelevant. Java always passes a copy of the value on the stack, that makes it pass-by-value.
dzonelurker replied ago:
"That's not what pass-by-reference means."
But what if we call it "pass-by-reference" from now on to avoid further confusion?
julian doherty replied ago:
Pass by "reference by value" is what I've always been taught. You have references to objects which you use in your local scope, but if you pass those objects into another method, you are actually passing a value copy of the reference pointing to the same object.
So you can mess with the object referenced in the argument in the second method, and it is the same object. But you cannot change the reference to point to another object and have it affect the reference in the first method - it just affects the reference copy used in the second method.
Fusion replied ago:
Guys, I have updated my original entry. I hope it makes it clear that we are actually discussing the same thing. I should have picked my title a bit more carefully tough.
,
Voters For This Link (6)
Voters Against This Link (23)