0

你能解释一下这个答案吗..?正如我所料,答案 C

 Given:
 11. class Snoochy {
 12. Boochy booch;
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. }

关于在第 23 行执行后立即被 snoog、snooch 和 booch 引用的对象,哪项陈述是正确的?

   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.

Answer: E
4

2 回答 2

3

这两个类相互引用。所以,当snoog被创建时,你也会得到 abooch和 a snooch。当你设置了snoog = null;booch并且snooch有资格获得GC

于 2013-04-25T06:36:29.980 回答
0

在第 23 行之后,没有指向Snoochy在第 22 行创建的实例的引用。=> 这个实例可以被垃圾回收。但是这个实例包含对 a 的引用Boochy(见第 13 行),没有其他引用存在。=:这也可以被垃圾收集。

于 2013-04-25T06:37:46.767 回答