Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Gc from JQuest

 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey java ppl
This one is from JQuest .
pls read the following code
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. }
When the program reaches line 6, how many of the String objects created in line 3 are eligible for
garbage collection? "Assume that the System.out object is not keeping a reference."
my answer is 11 but author says 10 can somebody explian how??(it print 0-10 , 11 objects )
I think if System.out is not keeping the object then it also has lost refernce to last object which makes it 11 ??
Regards Denish
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi denish,
i think that 10 objects will be eligible for garbage collection as the variable temp holds the reference to the last value i.e 0
Hope this answers the question
Samith.P.Nambiar
-----------------------------------------
The harder u try the luckier u get
-----------------------------------------
 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx buddy
I wonder how i missed that ?
Regards Denish
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Samith Nambiar:
hi denish,
i think that 10 objects will be eligible for garbage collection as the variable temp holds the reference to the last value i.e 0
Hope this answers the question
Samith.P.Nambiar

Hi Samith,
The existence of 'tmp' is only within the scope of the for-loop. So how could it hold the reference afterward, outside the loop?
Lam

[This message has been edited by Lam Thai (edited May 07, 2001).]
 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Lam The question only says uptill line 6 method returns on line 7.That's why
Regards Denish
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by denish mehta:
Hey Lam The question only says uptill line 6 method returns on line 7.That's why
Regards Denish

Denish,
Please re-read the original question:
When the program reaches line 6, how many of the String objects created in line 3 are eligible for garbage collection? "Assume that the System.out object is not keeping a reference."
It said, "when the program reaches line 6." Since 'tmp' is declared within the {} of the for-loop, at line 6, the scope of variable 'tmp' is gone. You don't need to wait until line 7.
On line #6 is where you have:
System.out.println("BOOM!");
If you replace that line with
System.out.println(tmp);
You will get a compiler error due to an undefined 'tmp'. Do you see the problem?
Take care,
- Lam -
[This message has been edited by Lam Thai (edited May 08, 2001).]
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi denish,lam
i'm sorry i got back so late.
read the article at this url :
<A HREF="http://www.lanw.com/java/localvariables.htm</a rel="nofollow">" TARGET=_blank>http://www.lanw.com/java/localvariables.htm
and please get back to this discussion with your respective views
thanx
Samith.P.Nambiar
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guys please get back to this discussion with your view ...
Samith.P.Nambiar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic