Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on Question from K&B

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ali.

Hope i will not get confused int he exam.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic