A question about Garbage Collection!

laura_zpf

Ranch Hand

Posts: 30

posted 16 years ago

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

Posts: 18944

posted 16 years ago

Laura, The question is not clear ! Where is the initial string referred by S?? The code u have given will not compile ! -sampaths