Hi leena rane:
This is from javaprepare site exam1
* 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
1. Before statement labeled 1
2. Before statement labeled 2
3. Before statement labeled 3
4. Before statement labeled 4
5. Never.
Ans
Now i get very confused whether lines such as 3 create new strings or refer to the same old String.There are some [b]other questions i have seen which i am not still sure of.
The correct answer is : before statement labeled 4
Because first you created two String objects pointed by r, s;
Then at line s = s + r; you created one more String object(new String object is created if you use "new keywords" and
"+ operator" with String ref)
The first object is pointed by s becomes available for garbage
collector
Hope this help
[This message has been edited by lu v thuan (edited October 26, 2001).]
Just remember, Strings are immutable. That means that once they are created they can't be changed. This refers to the actual String object in memory and not the reference variable that points to it.
SCJP2. Please Indent your code using UBB Code
Originally posted by suma krishnan:
In this case how many objects eligible for gc. My ans is one.somebody correct me if I am wrong.
SCJP2. Please Indent your code using UBB Code
Consider Paul's rocket mass heater. |