Hey everyone got a question on the following JQ+ question.
How many strings are eligible to be garbage collected after the execution of line 2 and line 4?
I said 1,3 the answer says 0,2 with this explaination
After execution of line 2, str0 points to "some string" and str1 becomes null. So "some string" is not hanging. Hence 0 objects are eligible for GC after line 2.
After execution of line 3, str2 points to str0 (ie. "some string") and "second string" becomes hanging.
After execution of line 4, str3 points to str2 (ie.again "some string") and "third string" becomes hanging. Hence 2 objects are eligible for GC after line 4.
My thinking is that on line 2 it sets str1 to null so that
String object is available to be gc'd. Then after line 4 the other 2 (str2 and str3)are eligibe to be collected because they go out of scope when the method exits.
The only point i'm not sure about is the literals in the pool, they aren't collected until the jvm exits right?
Thanks
Dave