JAVA中关于垃圾回收机制的问题

我的小黑 2008-07-23 05:14:10
先看这段代码:


1. class TestA {
2. TestB b;
3. TestA() {
4. b = new TestB(this);
5. }
6. }
7. class TestB {
8. TestA a;
9. TestB(TestA a) {
10. this.a = a;
11. }
12. }
13. class TestAll {
14. public static void main (String args[]) {
15. new TestAll().makeThings();
16. // ...code continues on
17. }
18. void makeThings() {
19. TestA test = new TestA();
20. }

有2个答案是正确的,应该是哪2个,给出解释!!!a和b哪个满足垃圾回收机制呢?
A. The object referenced by a is eligible for garbage collection.
B. The object referenced by b is eligible for garbage collection.
C. The object referenced by a is not eligible for garbage collection.
D. The object referenced by b is not eligible for garbage collection.
...全文
92 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
我的小黑 2008-07-23
  • 打赏
  • 举报
回复
This is a typical example of the island of isolation. On line 15, the two objects TestA and
TestB have a reference to one an other. Therefore, the correct answers are A. and B. A key
point to remember is that an object that is referenced by another object can be eligible for
garbage collection if the two objects form an island of isolated objects.
谢谢大家了,马上结贴!
ZangXT 2008-07-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ChDw 的回复:]
当然是AB,因为在// ...code continues on 这里,a 与 b没有没有额外的引用了,或者说从根对象开始的图已经无法到达 这两个对象了,所以可以回收
[/Quote]
正解,new TestAll().makeThings();之后就不存在对这个对象的引用了.
ChDw 2008-07-23
  • 打赏
  • 举报
回复
当然是AB,因为在// ...code continues on 这里,a 与 b没有没有额外的引用了,或者说从根对象开始的图已经无法到达 这两个对象了,所以可以回收
ZangXT 2008-07-23
  • 打赏
  • 举报
回复
a和b引用的对象都不能回收.
wangydong 2008-07-23
  • 打赏
  • 举报
回复
b,c
我的小黑 2008-07-23
  • 打赏
  • 举报
回复
给出解释吧~~~
ZangXT 2008-07-23
  • 打赏
  • 举报
回复
我认为是CD

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧