Friday, September 03, 2004

Java's pass "by reference" demystified (by Rahul)

So finally posting a blog after ages. The course in NCST has been great and I have learnt a lot. Clearing my fundamentals. Or really learning programming for the first time to put it correctly. The first module has been in Java programming and I read a few parts of "The Java Programming Language" by Ken Arnold and James Gosling.

Got an intersting part on passing parameters via methods. Personally I always thought that primitives were passed by value and instances by reference. But the second part is wrong. I'll simply write an excerpt from the book in full knowledge of the copyright violation!!

"..
You should note that when the parameter is an object reference, the object reference - not the object reference - not the object itself - is what is passed "by value". Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. But if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it. Here is an example to show this distinction:

class PassRef {
public static void main(String[] args){
Body sirius = new Body("Sirius",null);

System.out.println("before:" + sirius);
commonName(sirius);
System.out.println("after: " + sirius);
}

public static void commonName(Body bodyRef){
bodyRef.name = "Dog Star";
bodyRef = null;
}
}

This program produces the following output:

before: 0 (Sirius)
after: 0 (Dog Star)

Notice that the contents of the object have been modified with a name change, while the reference bodyRef still refers to the Body object even though the method commonName changed the value of its bodyRef parameter to null.
.."

"..
Some people will say incorrectly that objects in Java are "pass by refernce". The term pass by reference properly means that when an argument is passed to a function, the invoked function gets a refernce to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. If Java had pass-by-reference parameters, there would be a way to declare halveIt so that the preceding code would modify the value of one, or so that commonName could change the variable sirius to null. This is not possible. Java does not pass objects by reference; it passes object refernces by value. Because two copies of the same reference refer to the same actual object, changes made through one reference are visible through the other. There is exactly one parameter passing mode in Java - pass by value - and that keeps things simple.
.."

Did you guys know this?? Any more tips/hacks?

1 comment:

Anonymous said...

Yes - great post! One of the most misunderstood things in Java. And, to add to your post arrays are ALSO passed by value - so EVERYTHING is passed by value.

Good explanation here too:


Are arrays in Java passed by reference or value?