aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on Question  from K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on Question  from K&B" Watch "Doubt on Question  from K&B" New topic
Author

Doubt on Question from K&B

Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Hi
i am preparing for SCJP exam and came across this question from K&B



In the above code whenever i create an object for Cardboard it also creates an object for Short.So if i nullify the Cardboad Object reference then Short will also be eligible for garbage collection.

I am good so far.But what if instead of Short i have String literal in it?

I know String literals are not eligible for GC as they are in the pool until the class stays loaded inthe JVM.

So what should i choose if the question is to find the number of objects elligible for GC?

Also if i create String as new String("HI");

then what would be the answer for number of objects created?one or two?

if the question is to find number of objects elligible after nullify the reference to Cardboard Object that contains this new String("HI");

My asnwer would be two, one for the Cardboad object and another for the new String().i consider String literal will not be conisdered for GC.

Please help me and correct me i am wrong.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
My asnwer would be two, one for the Cardboad object and another for the new String().i consider String literal will not be conisdered for GC.

yup I guess your answer is right , however I don't think (hope) there will be any GC questions involving strings in the exam.

Check this out


SCJP 6
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Thanks Ali.

Hope i will not get confused int he exam.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
It's a good question, and the wonderful news is that, on the actual exam, objects of type String will never be used in GC questions.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on Question from K&B