aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question about Garbage Collection! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A question about Garbage Collection!" Watch "A question about Garbage Collection!" New topic
Author

A question about Garbage Collection!

laura_zpf
Ranch Hand

Joined: Sep 14, 2000
Posts: 30
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() {
r = r+1; //1
r = null; //2
s = s + r; //3String r = new String("abc");
String s = new String("abc");
} //4
a.Before statement labeled 1
b.Before statement labeled 2
c.Before statement labeled 3
d.Before statement labeled 4
e.Never.
The answer is d. But I think it should b. Because the two statements String r = new String("abc");String s = new String("abc");, the two "abc" is refer to different memory.The statement:r=r+1 lets r refer to a different memory, so I think the one of the "abc" should be collected.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Laura,
The question is not clear !
Where is the initial string referred by S??
The code u have given will not compile !
-sampaths
laura_zpf
Ranch Hand

Joined: Sep 14, 2000
Posts: 30
sorry, I make a mistake. The correct question should be following.
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about Garbage Collection!