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

garbage collection!!!

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q.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() {
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.
given answer is D,but mine is E.
I know i am wrong,please crrect me!!
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by junming zhang:
Q.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() {
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.
given answer is D,but mine is E.
I know i am wrong,please crrect me!!


s = s + r; returns a new String Object.
So, the old String is available for GC.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmm. I was thinking the answer will be B
After r = r+1; //Doesn't it return a new object (Stringbuffer.append().toString())??
String r = new String("abc"); and r = r+1; // different 'r' and different hashcode
Can some one clarify this for me?
Thankx
Ragu
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is asking about the reference s.
Not r.
r = new String("abc");
s = new String("abc");
not the same String object.
Not from a pool... like this
r = "abc";
s = "abc";
They are the same this way.

 
junming zhang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i see,
Best thanks for FEI NG )
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic