I am a software engineer at Google on the Android project and the creator of the Java testing framework TestNG. When I'm not updating this weblog with various software-related posts or speaking at conferences, I am busy snowboarding, playing squash, tennis, golf or volleyball or scuba diving. Cedric is a DZone MVB and is not an employee of DZone and has posted 90 posts at DZone. You can read more from them at their website. View Full User Profile

Value or Reference?

01.10.2011
| 8623 views |
  • submit to reddit

Death and Taxes are not the only two things that are certain in life: the regular resurgence of the “Is Java pass by value or pass by reference?” controversy comes to haunt us regularly as well. This year, it’s brought to us in the LinkedIn Java group and on TheServerSide.

Like all debates centered around words, it’s pretty hard to get a final resolution when most participants don’t agree on the meaning of these words.

This question makes for a great warm up interview question, though, but it would be pretty evil to ask it in these ambiguous terms, so instead, I simply prefer to show the candidate the two following snippets:

public void foo1(Person p) {
p = new Person("Joe");
}
public void foo2(Person p) {
p.name = "Joe";
}
and then ask them to explain what these two methods do.

The number of candidates who hesitate (and sometimes get it wrong) is pretty surprising. As is the amount of discussion that sometimes follows this question.

At any rate, the ensuing conversation is always interesting and it usually makes the candidate more comfortable for the rest of the interview.

From http://beust.com/weblog/2011/01/07/value-or-reference/

Published at DZone with permission of Cedric Beust, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Philippe Lhoste replied on Mon, 2011/01/10 - 7:22am

Ah, I was fearing it was yet another article explaining the difference...

I appreciate the example, it is simple and to the point.

I would probably have been in those giving a wrong answer not so long ago, and sometime, in complexer code, it can still be tripping... (unwanted and unseen side effect, particularly if you haven't written the code yourself).

Stephane Dupont replied on Mon, 2011/01/10 - 8:12am

If you look at this address: Java Tutorial they say:
"Primitive arguments, such as an int or a double, are passed into methods by value.
...
Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level."

Here is your answer

Slim Ouertani replied on Mon, 2011/01/10 - 11:54am

Yes, I always ask this question to new java trainee :

Yes reference is passed but with value so all is value :

http://www.jroller.com/ouertani/entry/10_questions_to_select_new

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.