This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
While calling you are essentially copying the reference value stored in variable a1 created on main() stack to a1 created on function() stack. Then the value of reference on function() stack is changed to point to a false, but the value of a1 on main() stack still remains the same to point to true.
Originally posted by Arul Jose: Since the object is passed by reference
You just found out that it isn't - it's the reference that's passed by value. The difference is subtle, but exactly what leads to the behaviour you observed.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
We are passing by value, meaning we are making a copy of "a1". In the new function body our "a1" (BTW: unfortunate coding to call it the same here) is *only* a copy of the a1 in the main() function. Everthing we do to this a1 (meaning the reference to the object that the other a1 reference outside the function body also points to) will not change anything in the object the other a1 (in main()) refers to. It's like saying, "Oh, I'll paint my new a1 reference red and hang a picture up on it." That won't change the old a1 reference.
BUT if we had an operator to change Boolean objects, we could use the "a1"-copy in the new function body to change the object that both a1's point to.
Is this correct? Or even helpful?
Joined: Feb 17, 2004
Wow, I read the stuff carefully this time and got scared for a minute. One thing is sure, java is call by value, parameters are copies. So when parameters are passed, copies are made of them, whether they are primitives or references (references are copied, not the objects). a1 in function is a copy (hence call by value) of the referenced object. But since you create a new object in function, a1 references that new object.
If you were to change the boolean value to false using a1 without creating a new object, you'd get true false. [ December 30, 2005: Message edited by: Dani Atrei ]
Joined: Jul 11, 2001
Originally posted by Dani Atrei: a1 in function is a copy (hence call by value) of the referenced object.
Be careful: it's a copy of the reference, referencing the same object. The object itself isn't copied - it isn't even passed into the method, strictly speaking, it just sits on the heap.
If Boolean had mutators (methods that change the state of the Boolean object), you could use those to change the original object. The assignement operator, on the other hand, doesn't do anything to an object, it just affects the reference.
Primitives are passed by value, too. The JVM makes a copy of the primitive and your parameter holds the copy. You can change it inside your method just like a local variable declared inside your method, but that is considered bad practice. In fact a lot of pretty smart folks recommend you make all parameters "final" so you can't change them. Try it once and see what the compiler tells you.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi