File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String object : Garbage Collection" Watch "String object : Garbage Collection" New topic

String object : Garbage Collection

Ritu Tandon

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
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.
I agree. Here's the link:
subject: String object : Garbage Collection
It's not a secret anymore!