Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Object Passing By Reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object Passing By Reference" Watch "Object Passing By Reference" New topic

Object Passing By Reference

Arul Jose
Ranch Hand

Joined: Jan 14, 2005
Posts: 131

Since the object is passed by reference, i expected the output to be


But it shows


Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
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.

Hence it prints true, true.

[ December 30, 2005: Message edited by: Mehul Sanghvi ]
Pratik Lohia
Ranch Hand

Joined: May 05, 2005
Posts: 88
Thats cos in the function you are createing anopther new object a1. Had you not created the new object reference , it would have printed false.
Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
This site is cool and touches a bit on your question:


Si altas son las torres, el valor es alto - Alberti
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
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
Stuart Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 169
Let me see if I have understood this correctly:

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?

Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
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 ]
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
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.

Hope this helps...
Joseph Sillitoe

Joined: Dec 30, 2005
Posts: 12
There is acctually a pretty good
article about this topic on this site:

To demonstrate what the Ilja and others are saying, here is an example using a mutable object (StringBuffer).

The output of the this code will look like:

Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
What about primitives?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
I agree. Here's the link:
subject: Object Passing By Reference
It's not a secret anymore!