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

# garbage collection!!!

Greenhorn
Posts: 8
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
i see,
Best thanks for FEI NG )

 Consider Paul's rocket mass heater.