C
cheka
Unregistered / Unconfirmed
GUEST, unregistred user!
对Java的garbage collection机制的认识比较模糊,它不同于Delphi或C++的析构,
今早上看了一道题,就更不明白了,谁来谈谈?
Q12.
1.public void count(){
2. for(int i=1;i<10;i++){
3. String tmp=Integer.toString(i);
4. System.out.println(tmp);
5. }
6. System.out.println("BOOM!");
7.}
When the program reaches line 6,how many of the String objects created in line 3 are eligible for garbage collection?(Assume the System.out object is not keeping a reference)
(a)None
(b)1
(c)8
(d)9
The answer mentioned is (c).Should'nt it be (d) because,8 objects are eligible for garbage collection when control is within the for loop scope.Once control moves out of the for block scope,the reference to the ninth object is lost thus,making the last String object(ninth object)too eligiible for garbage collection.
今早上看了一道题,就更不明白了,谁来谈谈?
Q12.
1.public void count(){
2. for(int i=1;i<10;i++){
3. String tmp=Integer.toString(i);
4. System.out.println(tmp);
5. }
6. System.out.println("BOOM!");
7.}
When the program reaches line 6,how many of the String objects created in line 3 are eligible for garbage collection?(Assume the System.out object is not keeping a reference)
(a)None
(b)1
(c)8
(d)9
The answer mentioned is (c).Should'nt it be (d) because,8 objects are eligible for garbage collection when control is within the for loop scope.Once control moves out of the for block scope,the reference to the ninth object is lost thus,making the last String object(ninth object)too eligiible for garbage collection.