• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

A question about Garbage Collection!

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.
void method X() {
r = r+1; //1
r = null; //2
s = s + r; //3String r = new String("abc");
String s = new String("abc");
} //4
a.Before statement labeled 1
b.Before statement labeled 2
c.Before statement labeled 3
d.Before statement labeled 4
e.Never.
The answer is d. But I think it should b. Because the two statements String r = new String("abc");String s = new String("abc");, the two "abc" is refer to different memory.The statement:r=r+1 lets r refer to a different memory, so I think the one of the "abc" should be collected.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Laura,
The question is not clear !
Where is the initial string referred by S??
The code u have given will not compile !
-sampaths
 
laura_zpf
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, I make a mistake. The correct question should be following.
void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
a.Before statement labeled 1
b.Before statement labeled 2
c.Before statement labeled 3
d.Before statement labeled 4
e.Never.
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic