Hi, 1. public void countDown() { 2. for( int i = 10 ; i >= 0 ; i-- ){ 3. String tmp = Integer.toString( i ); 4. System.out.println( tmp ); 5. } 6. System.out.println("BOOM!"); 7. }
At line 6 , how may objects are eligible for garbage collection? I chose 1 for tmp, but the answer says 10. Even if you include each of the Integer objects, the answer should be 11+1=12. 11 since the loop iterates 11 times.
------------------ Regards, Shree
Regards,<BR>Shree
anjan bhushan
Ranch Hand
Joined: Dec 12, 2000
Posts: 71
posted
0
Before line 6 it should be 11 at 6 should be 12
I am the most eligible bachelor in whole world, but only known in limited territory!!!
Digital Intoxication Blog
shree vijay
Ranch Hand
Joined: Sep 18, 2000
Posts: 208
posted
0
Hi, What about tmp. Can you elaborate? ------------------ Regards, Shree
anjan bhushan
Ranch Hand
Joined: Dec 12, 2000
Posts: 71
posted
0
Vijay I think only 11 will be eligible for GC.tmp wil hold one string
Ash Rai
Ranch Hand
Joined: Dec 15, 2000
Posts: 34
posted
0
Shree, The loop iterates 11 times and the 11th time String tmp holds Integer.toString(0). So there are 10 objects(Integer.toString(10).... Integer.to String(1)) which are eligible for GC.
anjan bhushan
Ranch Hand
Joined: Dec 12, 2000
Posts: 71
posted
0
what about "BOOM!"
shree vijay
Ranch Hand
Joined: Sep 18, 2000
Posts: 208
posted
0
The string tmp is declared within the for loop. At line 6 the for loop has ended and therefore, tmp is no longer in use. Doesn't that mean it is eligible for GC? Also, what happens when a method is quit -- is the memory for the local variables released automatically or they wait for the GC to free them?
umang bhartia
Ranch Hand
Joined: Sep 29, 2000
Posts: 60
posted
0
i agree with Ash, as it is asked number of objects eligible for GC at line 6, so the answer would b 10, as even though temp is out of scope in line 6, the local variable still has reference.
Hemant Patel
Greenhorn
Joined: Dec 15, 2000
Posts: 20
posted
0
hi Shree here tmp is declared in for loop so when loop iterates 11 time then that eleven. so their should be 11 object eligible for garbage after for loops block over (at line 6) Himanshu Amin