source:
https://coderanch.com/t/268621/java-programmer-SCJP/certification/many-objects-eligible-garbage-collection How many objects are eligible for garbage collection after line 1.
class
test {
public static void main(
String args[])
{
test t = new test();
String[] s=t.f(); //line 1
System.gc();
}
public String[] f()
{
String[] s = new String[4];
for(int i=0;i<s.length;i++)
{
s[i]=new String("" +i);
}
String[] s2 = new String[2];
s2[0]=s[0];
s2[1]=s[1];
return s2;
}
public void finalize()
{
System.out.println("ggg");
}
}
My answer is 5 objects.
object reference by s is local var in f().so scope of this variable with in the method only.when method execution is completed it is removed.
if s is unreferenced then elements in s are also eligible for GC.
totally 4 objects elgible for GC.
correct me if i am wrong.......