• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

GC - Bill Brodgen Exam Cram

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello all
Can someone tell me when will the String object created will be Garbage
Collected.
1 class Test
2 {
3 public static void main(String[] args)
4 {
5 String str = new String("Hello");
6 System.out.println(str);
7 }
8 }
I think after Line 6, It will be GC.
Does someone have differrent Answer.
Also look at the below code :-
This is from Bill Brodgen's Exam Cram - pg 163
1 class Test
2 {
3 public static void main(String[] args)
4 {
5for (int i = 10;i>=0;i--)
6{
7 String tmp = Integer.toString(i);
8System.out.println(tmp);
9 }
10 System.out.println("BOOM");
11 }
12 }
The Question asked is how many objects r GC after Line 10 ?
The Answer given was 10, & not 11 as it says that the local variable
still has a reference to the last String created.
What i don't understand is that in my first Example, if the object is
has no longer reference then it will be Eligible for GC which in the
example is at Line 6, i.e. after "System.out.println(str);"
So how come there is still a refernce in the Bill Brodgen's Example.
Thanks.
*/
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your first example, the String will have a reference until the main method exits and will never be garbage collected because the program will end.
The example of my question (which has caused endless confusion and argument) turns out to be pretty tricky. Basically it was a bad way to make a question about GC. In fact, using String objects in GC questions causes nothing but trouble.
See the following for a discussion: http://www.lanw.com/java/LocalVariables.htm
While at the lanw site, go to /books/errata
and get the total list of errata for the Exam Cram book.
Bill

------------------
author of:
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
answer Q#1 :
After line 7, not after line 6.
answer Q#2 :
I think because Bill said "after line 10", not "after line 11".
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all..
what i still don't understand is that how will be the no of variables eligible for GC be 10 and not 11 after line 10, since the tmp variable is local to the for loop and will be out of scope once the for loop ends..
pls help me out...

------------------
Hima
 
reply
    Bookmark Topic Watch Topic
  • New Topic