aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection Doubt" Watch "Garbage Collection Doubt" New topic
Author

Garbage Collection Doubt

sarvesh dikonda
Ranch Hand

Joined: Apr 08, 2012
Posts: 58

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.


Always believe in yourself
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Apr 08, 2012
Posts: 58

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

https://www.coderanch.com/how-to/java/CallByReferenceVsCallByValue


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

Joined: Apr 08, 2012
Posts: 58

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

It was really helpful and cleared all my doubts! Thank you very much Henry.
R Stokic
Greenhorn

Joined: Dec 11, 2012
Posts: 1

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

Joined: Apr 08, 2012
Posts: 58

Thank you R Stokic for your help too!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection Doubt