Topic: 一道scjp的题目!

  Print this page

1.一道scjp的题目! Copy to clipboard
Posted by: AchillesA
Posted on: 2005-07-16 19:32

QUESTION NO: 43
Given:

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. }
21. }

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.

麻烦给解释下!头都想大了,谢谢!
看道上面的题目让我想到了一点,就是this和使用new 产生一个实例有什么不同?

2.Re:一道scjp的题目! [Re: AchillesA] Copy to clipboard
Posted by: njluz
Posted on: 2005-07-17 13:26

我的理解就选C,D
理由是虽然A,B互相应用,但是A,B却没有任何第三方正在活跃的类使用了,garbage collection的处理机制可以收集这种情况内存。

3.Re:一道scjp的题目! [Re: AchillesA] Copy to clipboard
Posted by: AchillesA
Posted on: 2005-07-17 18:54

我的答案野生一cd 但是书上面给出的答案是c f

4.测试一下 [Re: AchillesA] Copy to clipboard
Posted by: 西曼
Posted on: 2005-08-09 10:23


package testApp;

class TestApp {
TestB b;
TestApp() {
b = new TestB(this);
System.out.println("Object A Init ed.");
}
protected void finalize(){
  System.out.println("DELETING Object A...");
}
}
class TestB {
TestApp a;
TestB(TestApp a) {
this.a = a;
System.out.println("Object B Init ed.");
}
protected void finalize(){
  System.out.println("DELETING Object B...");
}
}
class TestAll {
public static void main (String args[]) {
new TestAll().makeThings();
System.out.println("Calling System.gc()...");
System.gc();
for(;Wink;
}
void makeThings() {
TestApp test = new TestApp();
}
}

运行结果是:
Object B Init ed.
Object A Init ed.
Calling System.gc()...
DELETING Object A...
DELETING Object B...
结果表明答案应该选CD

5.Re:一道scjp的题目! [Re: AchillesA] Copy to clipboard
Posted by: aleel_008
Posted on: 2005-08-12 21:19

毫无疑问的问题,
b is eligible for gc

6.Re:一道scjp的题目! [Re: AchillesA] Copy to clipboard
Posted by: gavin_yue
Posted on: 2005-10-24 19:41

答案应该是CD 这是一个GC的典型情况--隔离岛


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923