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 Question of 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 "Question of garbage collection" Watch "Question of garbage collection" New topic
Author

Question of garbage collection

Carfield Yim
Greenhorn

Joined: Oct 20, 2002
Posts: 4
However, as String will pool the constant String for other string object to use. Thus I think there should be no String object are eligible for GC, am I right?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Strings that are created on the heap are eligible for garbage collection.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
On the heap

explanation, example, please...


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
String s1 = new String("Java"); // on the heap
String s2 = "Is Great"; // in the pool
All the methods of String that return a String object create a new String on the heap (if a new String object is needed).
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
just my $0.2...
A string created on the heap can be put into the pool by invoking intern().
Taking Thomas' example,
//s1 references a string object on the heap
String s1 = new String("Java");
//s2 references a string object in the pool
String s2 = "Is Great";
//s1 now references a string object containing the same character sequence as before, but located in the pool
s1 = s1.intern(); // in the pool
//s3 references the same string object as s1 (in the pool)
String s3 = "Java";
//this reference comparison yields true
System.out.println(s1 == s3);


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

Joined: Sep 26, 2002
Posts: 513
Thanks Valentin for pointing.


SCJP2, SCWCD<br />IBM Certified WebSphere<br />IBM Certified Solution Developer for WSAD
Ruff Young
Ranch Hand

Joined: Sep 26, 2002
Posts: 513
I am wondering that "s1" is no longer in heap?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The String is still on the heap but is now eligible for garbage collection since nothing references it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question of garbage collection
 
Similar Threads
StringTokenizer question
convert String into Object
proper Collection for my needs
what is string an object or literal ?
How to System.out.println the string reference