后天就要考scjp了。有个问题急!

alexsword111 2005-05-27 02:45:54
请教!!不是说对象为空的时候就可以收集了吗, 着是035的几道题 。

1.public class X(
2.public Object m(){
3.object o=new float(3.15F);
4 Object []oa=new object[1];
5. oa[0]=o;
6. o=null;
7. return oa[0];
8.}
9.}

when is the float Object created in line 3,eligible for
garbage collection?
A.Just after line 5
B.Just after line 6
C.Just after line 7(that is ,as the mothed returns)
D.Never in the Method .
Answer :D



12.void start(){
13.A a=new A();
14.B b=new B();
15.a.s(b);
16.b=null;
17.a=null;
18.System.out.print("start completed");
19.}

When is the B Object,created in line 14,eligible for garbage collection
A.After line 16
B.After line 17.
C.After line 18(when the mothed ends)
D.There is no way to be absolutely certain.
E.The Object is NOT eligible for garbage collection
Answer:C

10.public Object m(){
11.Object o=new Float(3.14F);
12.Object []oa=new Object[1];
13.oa[0]=o;
14.o=null;
15.oa[0]=null;
16.return 0;
17.}
When is the Float Object ,created in line 11,eligible for
garbage collection ?
A.Just after line 13.
B.Just after line 14.
C.Just after line 15.
D.Just after line 16(that is as the Method returns).
Answer:B




...全文
66 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
racingcode 2005-05-27
  • 打赏
  • 举报
回复
1.public class X(
2.public Object m(){
3.object o=new float(3.15F);
4 Object []oa=new object[1];
5. oa[0]=o;
6. o=null;
7. return oa[0];
8.}
9.}

when is the float Object created in line 3,eligible for
garbage collection?
A.Just after line 5
B.Just after line 6
C.Just after line 7(that is ,as the mothed returns)
D.Never in the Method .
Answer :D
根本没有对象符合GC的收集要求。虽然第六行时把o置为空,但在之前的第五行,已经把o原来引用的对象赋给了对象数组oa[0]中的元素,换句话说oa[0]也同样引用了o原来所引用的对象,因此,即使把o置为null,还是没有对象符合GC收集条件。
记住GC收集条件是:没有任何活的线程可以使用,到达一个对象时,才符合GC收集的要求。因此如果对象有引用的话,比如两个引用互相引用对方,即成了一个孤立的对象岛,也不会有任何LIVE线程可以到达使用,符号GC收集条件。

所以下边两个问题也是一样。你后天考SCJP?很巧,我也是后天考。GOOD LUCK!

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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