Topic: 做题求助 |
Print this page |
1.做题求助 | Copy to clipboard |
Posted by: lifecoolwind Posted on: 2005-02-23 16:16 QUESTION NO: 4 Given: 1. class Test { 2. private Demo d; 3. void start() { 4. d = new Demo(); 5. this.takeDemo; 6. } 7. 8. void takeDemo(Demo demo) { 9. demo = null; 10. demo = new Demo(); 11. } 12. } When is the Demo object, created on line 3, eligible for garbage collection? A. After line 5. B. After line 9. C. After the start() method completes. D. When the takeDemo() method completes. E. When the instance running this code is made eligible for garbage collection. Answer: E 此题为什么选E而不是选B (题出自上面那位贴出的testking) |
2.Re:做题求助 [Re: lifecoolwind] | Copy to clipboard |
Posted by: jsmile Posted on: 2005-02-24 09:17 demo in the parameter is just a copy of the reference d. So even demo is set to null, the Demo object that is created at line 4 is still referenced by d. Inside the method, if make any changes, say demo.setName("New Demo"); d will be effected. so d.getName() = "New Demo"; But if you set demo = null, only the copy of the reference d dies. If you're still confused, please check some topics on how Java does method calls, pass by value or reference. |
3.Re:做题求助 [Re: jsmile] | Copy to clipboard |
Posted by: lifecoolwind Posted on: 2005-02-24 16:00 thanks!!! it's indisputability to me now |
4.(One more please)Re:做题求助 [Re: lifecoolwind] | Copy to clipboard |
Posted by: lifecoolwind Posted on: 2005-02-24 16:08 Given: 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. return oa[0]; 16. } When is the Float object, created in line 11, eligible for garbage collection? A. Just after line 13. B. Just after line 14. C. Never in this method. D. Just after line 15 (that is, as the method returns). Answer: B why B but not C |
5.Re:做题求助 [Re: lifecoolwind] | Copy to clipboard |
Posted by: Seairy Posted on: 2005-03-04 08:33 11. Object o = new Float(3.14F); o指向的是3.14 12. Object [] oa = new Object[1]; 建立oa 13. oa[0] = o; 这时oa也指向3.14 14. o = null; o指向null 已经没有任何有效的引用对象 所以这行结束会被gc掉 |
6.Re:做题求助 [Re: Seairy] | Copy to clipboard |
Posted by: lifecoolwind Posted on: 2005-03-04 16:51 Post is deleted |
7.Re:做题求助 [Re: Seairy] | Copy to clipboard |
Posted by: michaelyung Posted on: 2005-07-19 11:44 11.o指向包装3.14的Float对象。 12.建立oa 13.oa也指向该Float 对象。 Float对象是在内存堆上分配好的。 o 和 oa都只是指向该对象的一个引用而已。 14. o=null;o这个引用没有了。 但Float对象还被另一个变量所引用着,那就是oa[0]; 所以该对象在这个方法中始终有变量引用它,所以不会被gc. Seairy wrote: |
8.Re:做题求助 [Re: lifecoolwind] | Copy to clipboard |
Posted by: jasonwijh Posted on: 2005-09-25 17:38 我认为答案应该是c才对 |
9.Re:做题求助 [Re: lifecoolwind] | Copy to clipboard |
Posted by: jaman Posted on: 2005-10-08 11:21 我也覺得是c正確,因爲“new Float(3.14F);”這個東西還有引用指著, 所以不應該gc掉! |
10.Re:(One more please)Re:做题求助 [Re: lifecoolwind] | Copy to clipboard |
Posted by: gavin_yue Posted on: 2005-10-24 19:33 Seairy 说的对~! lifecoolwind wrote: 这里有一个PASSING BY VALUE的问题 PASSING 的只是一个COPY 所以d只是把它引用的对象的一份COPY PASS到takeDemo(Demo demo)方法 至于takeDemo(Demo demo)方法怎么操作这份COPY 与d无关 lifecoolwind wrote: 而这道题 不涉及到这个问题 o和oa[0]引用的是同一个对象 其中一个对这个对象的操作 都会影响到另外一个 |
11.Re:做题求助 [Re: lifecoolwind] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-10-29 15:46 To gavin_yue and Seairy You both are very wrong! The answer is C. Even th method returns, the object is still be referenced. It is very easy to write a simple code to prove it. To prevent more debate on this simple and obvious question, I'll lock the thread. |
12.Re:做题求助 [Re: lifecoolwind] | Copy to clipboard |
Posted by: floater Posted on: 2005-10-29 21:41 I think we should let others discuss. There is always a start. |
13.Re:做题求助 [Re: floater] | Copy to clipboard |
Posted by: anleey Posted on: 2005-11-07 12:46 floater wrote: 就是啊。 |
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 |