Hi Mukti,
I think the correct answer is D.
The reason:
First of all an object is ready to be garbage collected once it is not referenced by any variable.
But here we are deling with Strings. Do u know the concept of STRING POOL.
A string pool is maintained by java which is checked whenever u create an object of type String. So if the value for which u r creating the String object exists in the String pool then a new object is not created instead the variable refers to the object which already exists.
e.g.
String str="hello";
String str1="hello";
here both these variable of string type refer to the same object in memory.
Here if I say
str1=null;
then the object "hello" is not available for garbage collection b'coz it is still referenced by str;
BUT YOU CAN CHANGE THIS BEHAVIOR USING new.
one u create a object using new then the String Pool is not checked and a new object is created.
e.g.
String str="hello";
String str1=new String("hello");
here both str and str1 store the same value but are seperate objects because 'new' forces the creation of new object.
PROBLEM ABOVE
in above problem first is created using 'new' so a seperate object is created for first and it is different from args[0] so when u say
first=null;
then the object is available for garbage collection.
So the correct answer is D.
This is my opinion.
In case I am wrong do inform me I shall be thankful
Neeraj Thakkar
[email protected]