SCJP中的一题(关于回收)

博客给出Java代码示例,包含TestA、TestB和TestAll类。提出在特定代码行后、main方法完成前的相关问题,询问哪些陈述为真。答案选C和D,解释称这是隔离岛的典型例子,相互引用的对象若形成隔离岛可被垃圾回收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class TestA {
 TestB b;
 TestA() {
 b = new TestB(this);
 }
 }
class TestB {
 TestA a;
 TestB(TestA a) {
  this.a = a;
  }
  }
class TestAll {
  public static void main (String args[]) {
  new TestAll().makeThings();
  // ...code continues on
  }
  void makeThings() {
  TestA test = new TestA();
  }
  }
 
Which two statements are true after line 15, before main completes? (Choose two)
A. Line 15 causes a stack overflow.
B. An exception is thrown at runtime.
C. The object referenced by a is eligible for garbage collection.
D. The object referenced by b is eligible for garbage collection.
E. The object referenced by a is not eligible for garbage collection.
F. The object referenced by b is not eligible for garbage collection.

答案是c,d。它是这样解释的:This is a typical example of the island of isolation. On line 15, the two objects TestA and TestB have a eference to one an other. Therefore, the correct answers are C. and D. 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值