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

String object : Garbage Collection

 
Ritu Tandon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we have :-

String s1 = new String("ABCDEFG");

I understand, there are two objects being created :-
(1)"ABCDEFG" -with no reference
(2)Second object "ABCDEFG" with a reference s1.

Say if the Garbage Collector runs,then does object(1)above ("ABCDEFG"- with no reference), gets garbage collected???
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first object is in the String pool and is never garbage collected, a least for purposes of the SCJP exam.
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy
if you take, how many objects will be eligible for GC just after this method returns?

public void compute(Object p)
{
String str = "abc";
}

"abc" is a string literal which goes to the string pool and is not GCed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic