This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
wei liu
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
wei liu
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic