object refered by 'a' is the only object in the method. 'x' is a variable of type int and 'str' refers to 'abc' which is string literal and will be in the string pool.
so there is only 1 object eligible for gc.
SCJP 1.6 96%
Joined: Feb 05, 2010
Hi Neha !!
Thanks for the reply.
So the Object a will be eligible for GC
on line 9 or at the end of the method on line 14 or
when the main method ends (Suppose i have called it from main())?
Joined: Oct 30, 2009
It will be available for gc at the end of method on line 14.
That's correct Trivikram Kamat, but, let's remove the 3,4 and 5 th lines in your above code.
Then, there will be 3 objects totally. One is in the String Constant Pool and other two are in the heap. [String Constant Pool is a special heap, leave it for a while]
Then at the 10th line, the two objects referred by str3 and str4 are only eligible for GC. Not the String Constant Pool objects even though it doesn't have explicit references.
Joined: Sep 26, 2010
Thanks Abimaran for your quick reply.
So, in the original question if we use String str = new String("abc");, the new String Object created on nonpool memory will be available for GC.
But, I still have one doubt.
Abimaran Kugathasan wrote:
Then, there will be 3 objects totally. One is in the String Constant Pool and other two are in the heap.
The one on the String constant pool ("Javaranch") is a literal, and not an object right?