File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection 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 "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
3. From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
[a] 1
[b] 2
[c] 3
[d] 0

Answer 3
i think the ans is wrong, the right should be d. what do you think? because string 2 is reassigned to string 1, string 1 is not null anymore.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
This a nothing to do with reassigning string2 to string1. The String created are String literals and String literals are not garbage collected but hold in the private String pool.
And even if the Strings were created dynamically (i.e. with the keyword new) there would be one String being garbage collected (Test) because on the third line string1 looses the reference to it and thus "Test" would be available for garbage collection.
To sum up,

no objects are garbage collected
BUT

string1 is available for garbage collection
Anywaym the answer provided is wrong !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
in your newly created code,
String string1 = new String("Test");
String string2 = new String("Today");
string1 = null; // here "Test" is available of garbage collection
string1 = string2;
finally, string1 repointed to string2 which is "Today", how could you say string1 is available to garbage collection?

Originally posted by Valentin Crettaz:
[B]This a nothing to do with reassigning string2 to string1. The String created are String literals and String literals are not garbage collected but hold in the private String pool.
And even if the Strings were created dynamically (i.e. with the keyword new) there would be one String being garbage collected (Test) because on the third line string1 looses the reference to it and thus "Test" would be available for garbage collection.
To sum up,

no objects are garbage collected
BUT

string1 is available for garbage collection
Anywaym the answer provided is wrong !
HIH
[/B]

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
it is not the reference (string1) that gets garbage collected but the object pointed to by it ("Test").
When you assign null to string1, the String "Test" is not pointed to by any reference (string1 or string2) anymore and thus it is available for garbage collection.

You can see that "Test" is not referenced by any reference. Assigning string2 to string1 has the effect of making string1 point to the object pointed by string2.
Remember that references are there for pointing to objects and are not themselves garbage collected.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 15, 2001).]
[This message has been edited by Valentin Crettaz (edited December 15, 2001).]
wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
thanks very much!!!

Originally posted by wei liu:
[B]in your newly created code,
String string1 = new String("Test");
String string2 = new String("Today");
string1 = null; // here "Test" is available of garbage collection
string1 = string2;
finally, string1 repointed to string2 which is "Today", how could you say string1 is available to garbage collection?


no objects are garbage collected
BUT

string1 is available for garbage collection
Anywaym the answer provided is wrong !
HIH
[/B]

[/B]
</BLOCKQUOTE>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection