• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
aresh babu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please clarify our doubt.....

Thanks in Advance....
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
aresh babu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic