Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Gc from JQuest

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx buddy
I wonder how i missed that ?
Regards Denish
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 >" 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
guys please get back to this discussion with your view ...
Samith.P.Nambiar
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic