wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Gc from JQuest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Gc from JQuest" Watch "Gc from JQuest" New topic
Author

Gc from JQuest

denish mehta
Ranch Hand

Joined: Dec 21, 2000
Posts: 135
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

Joined: Mar 14, 2001
Posts: 147
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

Joined: Dec 21, 2000
Posts: 135
Thanx buddy
I wonder how i missed that ?
Regards Denish
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
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

Joined: Dec 21, 2000
Posts: 135
Hey Lam The question only says uptill line 6 method returns on line 7.That's why
Regards Denish
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
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

Joined: Mar 14, 2001
Posts: 147
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

Joined: Mar 14, 2001
Posts: 147
guys please get back to this discussion with your view ...
Samith.P.Nambiar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Gc from JQuest