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: 1241
posted
0
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
posted
0
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: 1241
posted
0
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
posted
0
i see, Best thanks for FEI NG )
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.