After which line does the object created on line 1 is first eligible for GC?
I don't understand why the answer is 4 and not 2?
Local variable s in g() refers to the original object and if we set this reference to null, the original object without a referense is eligible for gc.
I am extremely sorry for my mistake. I was trying to post a tutorial link and I sent that image to my friend as a joke and posted the wrong link instead. I've rectified my mistake in the original post...
Joined: Feb 17, 2009
Oh, I get it.
If we do something with a copy of the original reference, it doesn't affect the original reference itselv.
Its an-object-passing-to-a-method question.
See what is happening here...
We create a array of Strings(containing 2 Strings) and refer it with a reference variable named s.Now we pass this ref var to g() .As we all know in java everything is passed using CALL-BY-VALUE so the method g() makes its own local reference variable of type String  and point it to the object created at line 1.Now inside g() the local reference variable s is assigned null but the s in the main() is still refering to the object created at line 1. g() returns the control to main().
Again the reference to object is passed to f() & f() creates its own local s and points it to the object created at line 1.Inside f() another String  is created (having 4 Strings) and f()'s local s refers to this new String  object. But again the object at line 1 is referred to by main() copy of s.f() returns to main() &
in main() a new String  object is created and main()'s copy of s now refers to this newly created object.Since its here at line 4, that the object from line 1 is inaccessible to the code. Hence making it eligible for garbage collection...
Hope this explanation solved your problem.. & yeah go through the String tutorial in ranch journal..Its quite helpful.