Link Details

Link 64566 thumbnail
User 263701 avatar

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.
  • 6
  • 23
  • 2427
  • 824

Comments

Add your comment
User 172008 avatar

Daniel Dyer replied ago:

6 votes Vote down Vote up Reply

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

User 270288 avatar

markturansky replied ago:

0 votes Vote down Vote up Reply

All objects are instantiated and live on the heap. References are passed around.
,

User 192671 avatar

ethzero replied ago:

1 votes Vote down Vote up Reply

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.

User 236137 avatar

dzonelurker replied ago:

0 votes Vote down Vote up Reply

"That's not what pass-by-reference means."

But what if we call it "pass-by-reference" from now on to avoid further confusion?

User 216692 avatar

julian doherty replied ago:

2 votes Vote down Vote up Reply

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.

User 263701 avatar

Fusion replied ago:

0 votes Vote down Vote up Reply

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.
,

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.