GeeCON Prague 2014*
The moose likes Mock Exam Errata and the fly likes JQ+   Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "JQ+   Garbage Collection" Watch "JQ+   Garbage Collection" New topic
Author

JQ+ Garbage Collection

Dominic Mack
Greenhorn

Joined: Sep 26, 2001
Posts: 17

The answer listed is line 5.
I'm not sure if this question is refering to the object reference 'b' or the actual object "world !".
If it is refering to the object reference 'b', it seems to me that it would not be available for gc till the method returns.
If it is refering to the actual object "world !"...from other posts I've read, when a string object is created it is saved for use later on and used again if the same string is referenced in code. (I know I'm not using the correct terms, excuse me)

[This message has been edited by Dominic Mack (edited October 12, 2001).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Dominic,

I'm not sure if this question is refering to the object reference 'b' or the actual object "world !".

Only objects are eligibile for garbage collection; not reference variables.

from other posts I've read, when a string object is created it is saved for use later on and used again if the same string is referenced in code.

It's true that String literal objects, like "world !" are stored in the string literal pool and are not eligible for garbage collection. The statement <code>String b = new String("world !");</code> actually results in two objects:

  • a string literal "world !" which is stored in the string pool, and
  • a new string object containing the unicode characters for "world !" stored in the general memory area or 'heap'

  • The reference variable 'b' would hold the memory address to the String object stored in the 'heap'.
    After line 5 in the example, no reference would be pointing to the 'heap' object so it would be eligible for garbage collection.
    Hope that helps.
    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
GeeCON Prague 2014
 
subject: JQ+ Garbage Collection