This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I've got a question about "http://www.java-help.com/Lesson16.htm". 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]
Karin, 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..."
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. Regards Ramki.
Joined: Apr 10, 2002
Good point, Ramki. The string literal will remain but the Double object will be garbage collected.