aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection!!!" Watch "garbage collection!!!" New topic
Author

garbage collection!!!

junming zhang
Greenhorn

Joined: Nov 03, 2001
Posts: 8
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!!


Dream first and Do later
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
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.
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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

Joined: Aug 26, 2000
Posts: 1242
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

Joined: Nov 03, 2001
Posts: 8
i see,
Best thanks for FEI NG )
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: garbage collection!!!