This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Well, all in favour of FAQs, except when they're misleading...
In short in java, object references are passed by reference and primitive types are passed by value.
What? Java always passes by value.
Object references are passed by value. The callee gets a new copy of the object reference, which the callee may change without affecting the caller.
In Java you can't have variables that are objects. You can only have variables that are object references.
Some people like to say that, when passing an object reference by value to a method, they are effectively passing the object by reference. If you're one of those people, the FAQ could more correctly say: -
In short in java, objects are passed by reference and primitive types are passed by value.
[ May 03, 2007: Message edited by: Peter Chase ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joined: Mar 22, 2005
Peter, since you have already worked your way through the FAQ, may I ask that you amend it to reflect your points?
(I admit that I hadn't actually read it, just pointed to it because I knew it was out there.)
Joined: Oct 30, 2001
I could do so, but how?
The FAQ looks fine to me, except for the one sentence mentioned in my previous post. I already included a suggested revision for that one sentence.
"Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost."
"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."