Interview question: Objects eligible for garbage collection

前端 未结 7 885
无人共我
无人共我 2020-12-02 19:03

Give the following code:

class A {
    Boolean b;
    A easyMethod(A a){
        a = null;
        return a;
    }
    public static void main(String [] args         


        
7条回答
  •  伪装坚强ぢ
    2020-12-02 19:48

    Your question is quite confused.

    The garbage collector works on objects, not on variables. So when you say that a2 is eligible for GC it mean nothing. You should say the object referenced by a2 at line N is eligible for GC at the line N+M.

    In your example you only have 3 objects being instantiated. It is the first create A and the last create A instance that are eligible for GC.

提交回复
热议问题