wood burning stoves 2.0*
The moose likes Java in General and the fly likes Is Java language works as pass by reference mechanism in case of objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is Java language works as pass by reference mechanism in case of objects?" Watch "Is Java language works as pass by reference mechanism in case of objects?" New topic
Author

Is Java language works as pass by reference mechanism in case of objects?

aresh babu
Ranch Hand

Joined: Aug 31, 2008
Posts: 65
Hi All,

Please clarify our doubt.....

Thanks in Advance....
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2274
    
  28

Remember that when you copy a variable, you are copying a reference. So, there are 2 references to the same object. Making one null, doesn't make the other null. So, if you do this



At this point, c2 still has reference to the object, even though c1 dropped it.

Also, if you have 2 references to the same object and you change the state of the object through one reference, both references will see the change



Now if you check c2.story, it will be 100
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Java passes only by value.

Primitives are passed by value.
References are passed by value.
Null is passed by value.

Nothing is passed by reference.

Objects are not passed at all--not by reference nor by value nor by carrier pigeon.

Note that passing a reference by value is not the same as passing an object by reference, though they may appear similar on the surface.

Here are this site's tutorials on the subject:
http://www.javaranch.com/campfire/StoryCups.jsp
http://www.javaranch.com/campfire/StoryPassBy.jsp

I prefer to think of it this way: A reference variable is a piece of paper with latitude/longitude coordinate written on it that tell me where a particular house is. When I pass that variable to a method, I copy the coordinates from my piece of paper onto another piece of paper, and hand that second piece of paper to the method. I have my paper and the method has his, and they both have the same value (the same coordinates) written on them. We can each use our own paper to find the same house.

If he sets his variable to a different value (erases what's on his paper and writes the coordinates of a different house), he's just pointing to a different object. He hasn't change the value of my variable (hasn't changed what's written on my paper) and he hasn't change the state of the object he was pointing to (hasn't changed the furniture or the color of the walls in the house at the original coordinates).
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

also from the FAQ .... http://www.coderanch.com/how-to/java/CallByReferenceVsCallByValue


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
aresh babu
Ranch Hand

Joined: Aug 31, 2008
Posts: 65
Hi All,

Thanks alot for your valuable explanation....

And also another doubt is if the code reaches to the place //do morestuff in the above programm, how many objects will be eligible for garbage collection???


Thanks in Advance
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

aresh babu wrote:Hi All,

Thanks alot for your valuable explanation....


You're welcome.

And also another doubt is if the code reaches to the place //do morestuff in the above programm, how many objects will be eligible for garbage collection???


How many do you think, and why?

Also, in the future, when posting code, please UseCodeTags(⇐click) so it will be readable. I've added them to your original post.
aresh babu
Ranch Hand

Joined: Aug 31, 2008
Posts: 65
I think totally 3 objects are eligible for garbage collection.

1. object C3 which is created in main()

2. Integer a which is created in go()

3. c3.story because story is also a wrapper object which is created with object c3

Please correct me if i am wrong...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

aresh babu wrote:I think totally 3 objects are eligible for garbage collection.


First, note that when we reach the // do stuff line we are still inside the main method, and all of its variables are still in scope.

1. object C3 which is created in main()


There's no object created there. c3 just holds a copy of the reference returned by the go() method. But even if c3 were the only reference to that object, c3 is still in scope, so the object it points to is not GC-eligible.

2. Integer a which is created in go()


Right.

3. c3.story because story is also a wrapper object which is created with object c3


No. c3 points to the same value as c2. Both c3 and c2 point to the object created by CardBoard c2 = new CardBoard(); because go() returns a copy of the reference that was passed to it.


Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Jeff Verdegan wrote:No. c3 points to the same value as c2.

No it doesn't. The go method always returns null, so c3 never points to any object.


Joanne
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Joanne Neal wrote:
Jeff Verdegan wrote:No. c3 points to the same value as c2.

No it doesn't. The go method always returns null, so c3 never points to any object.


Oops. My bad. Must've gone crosseyed reading that, scrolling up and down between the code and my comments.

@aresh babu: My comment above that Joanne corrected was in response to you saying c3.story would be eligible for GC. Since c3 is null, there is no c3.story.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Java language works as pass by reference mechanism in case of objects?
 
Similar Threads
object eligible for GC
Chapter 3 - SELF TEST - Page 269
garbage collection
how many objects are eligible for GC
Bug test chapter 3, question 1?