Link Details

Link 312839 thumbnail
User 410289 avatar

By CodeJustin
via javadude.com
Published: Dec 06 2009 / 22:22

I finally decided to write up a little something about Java's parameter passing. I'm really tired of hearing folks (incorrectly) state "primitives are passed by value, objects are passed by reference". I'm a compiler guy at heart. The terms "pass-by-value" semantics and "pass-by-reference" semantics have very precise definitions, and they're often horribly abused when folks talk about Java. I want to correct that... The following is how I'd describe these
  • 55
  • 6
  • 6852
  • 6

Comments

Add your comment
User 388907 avatar

MCII replied ago:

1 votes Vote down Vote up Reply

How often has this been posted before?

User 277934 avatar

jfpoilpret replied ago:

0 votes Vote down Vote up Reply

Not new, maybe. But there's till a lot of confusion about this among many Java developers (in particular, younger people). Hence it is worth repeating. In addition, this post is well written and does a lot to make those engineers understand why Java is pass-by-value only.

User 294184 avatar

mrjohnson replied ago:

2 votes Vote down Vote up Reply

Yeah, it's one of those things that confuses people quickly. This is a pretty good explanation, I thought.

User 254602 avatar

Nick Brown replied ago:

0 votes Vote down Vote up Reply

Yeah, but those developers won't be reading this.

User 358026 avatar

franz.see.myopenid.com replied ago:

3 votes Vote down Vote up Reply

Not often enough. This is one of the most confusing things to people who don't understand pointers. And I found myself explaining myself to senior java developers for 30 to 60minutes...trying to convince them that java IS pass by value.

I can't blame them though, what good does that knowledge bring? Aside from the fact that you can't change the value of the variable passed to a method - not much :-)

User 254602 avatar

Nick Brown replied ago:

0 votes Vote down Vote up Reply

Except they can change the value of anything within a mutable object whose reference was passed by value. Assuming they don't first change the value of the reference.

Weee, the fun of programming in Java never stops.

User 236075 avatar

henk replied ago:

0 votes Vote down Vote up Reply

>How often has this been posted before?

A couple of times really, but some people keep thinking Java is call-by-reference.

Sometimes beginners think this, and I can understand that. It might a confusing thing to learn when you're new to this and they just need to be educated. Unfortunately, sometimes seasoned Java developers, even those famous from some well-known open source application, keep spreading the idea that Java is in fact call-by-reference and deny to accept to truth that Java in fact passes pointers by-value.

User 236075 avatar

henk replied ago:

2 votes Vote down Vote up Reply

An interesting discussion about this appeared here: http://www.theserverside.com/news/thread.tss?thread_id=39536

After some fierce initial debate, including [b]Jacob Hookom[/b] of JSF fame as a "java has-call-by-reference semantics" proponent, everybody seemed to eventually settle down on the agreement that Java indeed passes pointers by-value.

User 393686 avatar

RawThinkTank replied ago:

1 votes Vote down Vote up Reply

Damn, no pointers even.

User 547789 avatar

thethoks replied ago:

0 votes Vote down Vote up Reply

Nice article

User 236075 avatar

henk replied ago:

2 votes Vote down Vote up Reply

POINTERS-THAT-ARE-PASSED-BY-VALUE

That's what Java uses for normal local calls for objects.

Look at the following:

public void someMethod() {
SomeObj test = new SomeObj();
SomeObj cmp = test;

change(test);

// Other methods can access what "test" points too.
// Other methods can change the content of whatever object "test" point too.
// But no method can let "test" pointing to another object
assert(test == cmp);
}

User 170066 avatar

DimitrisAndreou replied ago:

-2 votes Vote down Vote up Reply

Gee. He could have just google and link to the first few results instead. This is sooo '97.

User 494381 avatar

sharpaw replied ago:

-3 votes Vote down Vote up Reply

java is using reference and values for methods. of course, if you start creating your own dictionary on what reference means and what value means then you can say java is not passing parameters at all. i think everybody should stick to the original meaning of the words ... why? well... if you go to an interview and you`ll tell that java is only using "values", well, it`s pretty clear that you`ll not get the job.. just my opinion (that`s why I voted down)

User 294184 avatar

mrjohnson replied ago:

1 votes Vote down Vote up Reply

You read all that and the discussion and decided he was making up words?

User 350084 avatar

Joshua.Suereth replied ago:

0 votes Vote down Vote up Reply

Ha... so it uses call-by-value to pass around.... references!!!

That's right, Reference is used as the term for a reference to an object in Java, i.e. Object foo = new Object() -> foo is the reference to the actually allocated object. I think this is why people get confused about "call-by-reference". It's really call-by-value that is copying the reference....

User 236075 avatar

henk replied ago:

0 votes Vote down Vote up Reply

>Ha... so it uses call-by-value to pass around.... references!!!

Exactly, passing references by value is subtly but crucially different from passing by reference.

As the author of the text rightfully explains, the majority of this confusion originates from the fact that Java started calling its pointers references. This was a marketing driven decision, needed to distinguish itself from C++. At the time, the whole Java strategy was based on being the 'easy alternative to C++', so from a marketing point of view making up a different name for "pointer" was of the uttermost importance.

Of course, when marketing interferes with deeply technical matters of which they have no idea what is actually means, is a sure recipe for trouble.

I love Java, but the term "reference" should really be abolished. Its wrong, confusing and plain stupid. It's a little late in the game to still change this, but on the other hand the strategy of having to be the easier alternative to C++ is completely not relevant anymore. So now might be a good time to change this. Sun had the balls to drop the idiotic "2" in the Java name, so let's take it to the next step...

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.

Play Framework
Written by: Ryan Knight
Featured Refcardz: Top Refcardz:
  1. Akka
  2. Design Patterns
  3. OO JS
  4. Cont. Delivery
  5. HTML5 Mobile
  1. Akka
  2. JUnit/EasyMock
  3. Java Performance
  4. REST
  5. Java