• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Garbage Collection Doubt

 
Ranch Hand
Posts: 58
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below code snippet is from SCJP 6 Study Guide -



Explanation as per the book: Option C:- 2 objects are eligible for GC
C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible.

Can someone among the ranchers, please help me understand why CardBoard c2 is NOT eligible for Garbage Collection as the copy of reference has been sent to go method in which c2 is assigned null.So, c3 is pointing c2 isn't it? I am really confused.

There's one more in the same chapter which is just the same-



Output is: 4 4

Here the explanation is given as -
A is correct. Although main()'s b1 is a different reference variable than go()'s b1, they refer to the same Box object.

My confusion is that, as both of the above snippets depict the same scenario in terms of invoking and pointing to the same object , then why the difference. Please give me SOLID explanation/RULE so that i can never get confused later and understand thoroughly so that it gets burned into my brain.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Sarvesh. you need to understand how the arguments gets passed to methods in java. if you are aware of C++ you might remember we used to talk about Pass By Value or Pass by Reference . so what is the case in java. if you want to BURN something in your mind(as you say) then burn that java is PASS BY COPY OF VALUE. what does that mean ? let me explain using your CardBoard example.

Consider the statement CardBoard c3 = go(c2);

now according to you c2 gets passed in go method which assigns it to null. this is wrong. actually a copy of c2 is made and that copy is passed to go method. so in go method that copy of c2 is assigned to cb which gets initialized to null. so as you say c2 is NOT null. c2 still points to CarbBoard.

Same thing with the box example. i left it to you to figure it out .

Regards
Gurpreet
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:hi Sarvesh. you need to understand how the arguments gets passed to methods in java. if you are aware of C++ you might remember we used to talk about Pass By Value or Pass by Reference . so what is the case in java. if you want to BURN something in your mind(as you say) then burn that java is PASS BY COPY OF VALUE. what does that mean ? let me explain using your CardBoard example.

Consider the statement CardBoard c3 = go(c2);

now according to you c2 gets passed in go method which assigns it to null. this is wrong. actually a copy of c2 is made and that copy is passed to go method. so in go method that copy of c2 is assigned to cb which gets initialized to null. so as you say c2 is NOT null. c2 still points to CarbBoard.

Same thing with the box example. i left it to you to figure it out .

Regards
Gurpreet



Hello Gurpreet. First of all thank you for being patiently reading my query as its too long & responding back to it. Now, if i go by the rule you stated 'PASS BY COPY OF VALUE' then, if i apply to the second code snippet i.e of the Box[], a copy of value (in this case copy of b1 which is pointing to a Box object on heap with value 5 initialized ) is sent to the go() method,okay. That means as per your statement, changes to the copy does not affect the ORIGINAL one. As in the earlier example, ORIGINAL c2 is not set to NULL in spite of the copy being set to NULL. Now, why the same RULE, in this case, doesn't seems to APPLY. Change made to copy of b1 on line 14 i.e. b1.size=4 should not affect the value of size of the ORIGINAL one of b1 i.e the one on line 8 Box b1 = new Box(5); Don't think that you are not able to help me properly.All your efforts are appreciated Gurpreet. Its me who is not getting this thing, right. Kindly try once more. Others too can help
 
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/how-to/java/CallByReferenceVsCallByValue
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:https://coderanch.com/how-to/java/CallByReferenceVsCallByValue


It was really helpful and cleared all my doubts! Thank you very much Henry.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think you are confused about how reference variables work. Reference variable contains "address" or a value that represents the way how to get some object on the heap. So, when you PASS A COPY of that value to another reference variable or method argument you end up with two reference variable that are referring to ONE object and you can make change to this SINGLE object with either one of those two references.

eg. loclal variable Box b2 and and ma[0] are referring to the same object and you can change value of size variable with either of those references.

In the first snippet, you pass copy of c2 reference to method go(c2) and then later change it's value to null, but there is still original of that value stored in c2 reference that is still referring to CardBoard object so it's not eligible for GC .

I hope this is that SOLID explanation/RULE that you are looking for.
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you R Stokic for your help too!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic