This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Hi Sunita, Think the following 3 items are available for garbage collection: str1 - set to 'null' in Line 2. Any object set to 'null' is eligible for garbage collection. The literal strings "second string" and "third string" as they are no longer being referenced. (Not 100% sure of these, as they would be in the string pool and I'm not clear on gc and the string pool).
I would agree with Manish. also Jane, even if we are setting str1 to null in line 2, the String object that str1 was refering to ( ie. "some string") is still being refered to by str0 now. So that object will not be Garbage Collected.
String str0 = null; String str1 = "some string"; String str2 = "second string"; String str3 = "Third string"; str0 = str1; // 1 str1 = null; //2 str2 = str0; //3 str3 = str2; // 4 According to me, at line 4 , this will be the case: str1 str0 str2 str3 | \ | / null Some string therefore, second string and third string will be ready for garbage collection.
For your information, string literals( constructed using the = operator instead of new() ) are stored in the String pool. These strings are re-used if the program tries to create identical strings. Strings in the string pool live longer than the objects created using the new() operator. The JVM garbage collects the strings in the pool when the class is unloaded. Please note that you are not required to know about GC of string literals for the exam. The GC-related questions in the exam almost always deal with regular new()'d objects. However, if in case questions do appear about GC of string literals, treat them as any regular object. Hope that helps, Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).