I got a question about the following piece of code.
class MemArr{
int[] mem = {1,2};
}
public class Test{
public static void main(String[] args){
MemArr[] x = new MemArr[2]; //line 1
x[0] = new MemArr(); //line 2
x[1] = null; //line 3
//line 4
}
}
How many objects are created in total and how many objects are eligible for garbage collection when line 1 is reached?
I think, that at line 1, there were 5 objects created (1 array x, 2 objects of MemArr in the array x, 2 member variables of integer array for the objects of MemArr)
At line 2, two objects were created (one MemArr object and its member variable mem).
At line 3 when x[1] is set to null, I think that there were in total 4 objects which are eligible for the GC, but I am not very sure about this.