This is question from dan chisholm
class Q {
private int id;
protected void finalize() {System.out.print(id);}
public Q(int i) {id = i;}
}
class R {
public static void main(
String[] args) {
Q q1 = null;
for (int i = 0; i < 10; i++) {q1 = new Q(i);} // 1
System.gc(); // 2
}}
When the processing of line 2 begins, how many objects of type Q that were created at line 1 have become eligible for garbage collection?
The answer given is 9 with explanation:
With each pass through the loop, q1 references a new object, and the old object becomes eligible for garbage collection. When the processing of line 2 begins, the last object referenced by q1 is not eligible for garbage collection.
my doubt is ,Is there a possibility of runing garbage collector before calling System.gc() in line number 2, so that the answer may be less than 9.