gpt4 book ai didi

java - SCJP测试题

转载 作者:行者123 更新时间:2023-11-30 06:38:46 24 4
gpt4 key购买 nike

SCJP 测试题:

 class A{
A aob;
public static void main(String args[]){
A a=new A();
A b=new A();
A c=new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d=new A().aob=new A();
c=b;
c.aob=null;
System.gc();
}
}

问题:c.aob=null执行后,有多少对象符合垃圾回收条件。

我认为是 1,但正确答案是 2。有什么问题吗?

最佳答案

我通过谷歌搜索找到了this thread .

The first object is the one referenced originally by c.

A a= new A();
A b= new A();
A c= new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d= new A().aob=new A();
c=b; //(1)

It becomes eligible at (1).

The other object eligible for GC is at statement

A d=new A().aob=new A();

Here the object created in the text in bold will be eligible for GC. The object in italicized text will be assigned to d.

关于java - SCJP测试题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1900443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com