This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String object : Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String object : Garbage Collection" Watch "String object : Garbage Collection" New topic
Author

String object : Garbage Collection

Ritu Tandon
Greenhorn

Joined: Dec 05, 2003
Posts: 3
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

Joined: Mar 13, 2004
Posts: 1272
The first object is in the String pool and is never garbage collected, a least for purposes of the SCJP exam.


Mike Gershman
SCJP 1.4, SCWCD in process
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
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.
 
Don't get me started about those stupid light bulbs.
 
subject: String object : Garbage Collection
 
Similar Threads
what is Stringpool and heap
Garbage collection
Shifting and Garbage Collection
Garbage Collection
String objects