SCJP 1.6版考題 122
出自 陳富國維基館
Given: 11. public void genNumbers(){ 12. ArrayList numbers = new ArrayList(); 13. for(int i=0; i<10; i++){ 14. int value = i * ((int)Math.random()); 15. Integer intObj = new Integer(value); 16. numbers.add(intObj); 17. } 18. System.out.println(numbers); 19. } Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for garbage collection? A. Line 16 B. Line 17 C. Line 18 D. Line 19 E. The object is NOT a candidate for garbage collection.
解答
Ans: D
解說:
intObj是宣告在for迴圈裏,這個迴圈結束之後(intObj消失),看起來15行所建的物件都要能回收, 但是這些物件被加到numbers的ArrayList(集合物件)裏,這些物件可以透過numbers被參考到, 直到numbers這個參考變數消失,也就是genNumbers方法結束。
|