• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question on GC frm go4java mock test

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After which line the object initially refered by str is eligible for garbage collection?Select one correct answer.

1 class Garbage
2 {
3 public static void main(String arg[])
4 {
5 String str=new String("Hello");
6 String str1=str;
7 str=new String("Hi");
8 str1=new String("Hello Again");
9 return;
10 }
11 }


My answer for this is "After line no. 8 ". Is this correct ?? If not please say what the correct answer is and the explanation.

I was searching thru the post to see if this question was already asked, but couldn't find one. In the process of searching found some articles on String and it is different than other object with respect to GC, can anyone explain it again..

Thanks,
JP
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too think the same way you have thought !!!

Cheers,

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

You are right...

At line 6 ///there are two references for that object...

//line 7 only reference remains that of str1

//after line 8 ..even this points to another location...

So no more references exists for the object and eligible for GC..

 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I go for 8.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that After line 7 the initially referred object by str is eligible for GC. I am not sure.

All of you were said it is after 8.
Please explain.

Sampath
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the above post..



At line 6 ///there are two references for that object...

str and str1 are pointing to the same object So 2 references remain

//line 7 only reference remains that of str1

Here str is now pointing to a different and new String object and
str1 is still pointing to the old object.. So only 1 reference remains

//after line 8 ..even this points to another location.

Here str1 is pointing to a new string object...and the old object now has no reference..So 0 reference...

Hope you get it now...




 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic