This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collector  RSS feed

 
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey The following is the code:-





COuld anyone tell me with appropriate reasons where the Garbage Collector can collect any object....... in this code......


Early responses are awaited...........

 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can solve these type of questions pictorially. Thats the best way represent each object with a circle , assignment statements wit a link to the object.

So in line 2 a new String object is created and it points to this object
In line 4 another string reference variable points to the object created in line 2
Now we have two links or references to the string object.

At line five only one string reference is set to null the other reference to the string object still exists and so this string object is not eligible for GC

COuld anyone tell me with appropriate reasons where the Garbage Collector can collect any object....... in this code......

Even if the two string references are set to null we can't say whether the Garbage collector will collect it, we can only say the object is eligible for GC.
[ April 28, 2006: Message edited by: Srinivasa Raghavan ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!