• 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

gabage collection

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
in the following code

1. class myclass
2.{
3. public static void main(String [] args){
4. string s1 = "gabage"
5. string s2 = "collect"
6. string s3=s1+s2;
7. s1=null;
8. s1=s3
9. s2=s1;
}}
What is the ealiest point when the memory allocated to s1 can be realsed is it line 7 or after line 9
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.
Please notice that your displayed name doesn't comply with the naming policy. Please adjust to it.
Let's suppose the two variables were created with new. In that case the answer is 7, because so far the only refererence to the object was s1; and this reference is lost via the assignation of null.
In the exam you will not see a question about eligibility of string literals for g.c. You will only see "normal" objects. In fact the correct answer is that the objects computed from string literals are not normally g.ced
 
I think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic