File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection!!!

 
junming zhang
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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!!
 
Fei Ng
Ranch Hand
Posts: 1244
1
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • 0
  • Mark post as helpful
  • send pies
  • 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: 1244
1
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see,
Best thanks for FEI NG )
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic