Explicit nulling

前端 未结 7 1208
再見小時候
再見小時候 2020-12-14 23:17

In what situations in java is explicit nulling useful. Does it in any way assist the garbage collector by making objects unreachable or something? Is it considered to be a g

相关标签:
7条回答
  • 2020-12-15 00:09

    From "Effective Java" : use it to eliminate obsolete object references. Otherwise it can lead to memory leaks which can be very hard to debug.

    public Object pop(){
        if(size == 0)
            throw new EmptyStatckException();
        Object result = elements[--size];
        elements[size] = null; //Eliminate Object reference
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题