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 Ready for 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 "Ready for garbage collection" Watch "Ready for garbage collection" New topic

Ready for garbage collection

Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
I've got a question about "".
An object is automatically "garbage collected" when it can no longer be referenced.
double x = new Double("123.45").doubleValue();
The Double object is never assigned to an object reference. At what moment will the Double object be ready for garbage collection?

I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
Jon Huhtala

Joined: Apr 10, 2002
Posts: 17
Technically the object that was constructed by the
new Double("123.45)
is "eligible" for garbage collection immediately after the statement is executed. There is no way to tell exactly when the JVM will dispose of the object. It does it when it is darn good and ready...

- Jon Huhtala, SCPJ2<p> "Nothing worthwhile is ever easy..."
ramki srini

Joined: Aug 27, 2002
Posts: 26
double x = new Double("123.45").doubleValue();
If we take "123.45" as String literal, then I don't think it will be garbage collected anytime.
All String literal will be kept in the String pool and will be used by the JVM in all future references for the same literal.
I hope I am right, but I am not sure. Please correct me if I am wrong.
Jon Huhtala

Joined: Apr 10, 2002
Posts: 17
Good point, Ramki. The string literal will remain but the Double object will be garbage collected.
I agree. Here's the link:
subject: Ready for garbage collection
It's not a secret anymore!