class Snoochy {
12. Boochybooch;
13. public Snoochy() { booch = new Boochy(this); }
14. }
15.
16. class Boochy {
17. Snoochy snooch;
18. public Boochy(Snoochy s) { snooch = s; }
19. }
And the statements:
21. public static void main(String[] args) {
22. Snoochy snoog = new Snoochy();
23. snoog = null;
24. // more code here
25. }
Which statement is true about the objects referenced by snoog,
snooch, and booch immediately after line 23 executes?
A. None of these objects are eligible for garbage collection.
B. Only the object referenced by booch is eligible for garbage
collection.
C. Only the object referenced by snoog is eligible for garbage
collection.
D. Only the object referenced by snooch is eligible for garbage
collection.
E. The objects referenced by snooch and booch are eligible for garbage
collection.

解决方案 »

  1.   

    个人认为,应该选C。因为执行Snoochy snoog = new Snoochy();后,snoog指向一个对象(假设为a),booch指向一个对象(假设为b),snooch指向一个对象(假设为c)。在执行完snoog = null;后,只有a没有引用指向它,booch还指向b,snooch还指向c,所以GC就只回收a。
      

  2.   

    E. The objects referenced by snooch and booch are eligible for garbage
    collection.两个都能被回收, 因为没有被其他对象引用了
      

  3.   


    23. snoog = null;
    这句不是表示snoog没有引用了,为什么不能被回收呢?
    另外两个在环内互相引用,为什么就能被回收呢?