Why this String is not eligible for garbage collection?
<p>The following code creates one array and one string object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection?</p>

<pre><code>...
String[] students = new String[10];
String studentName = "Peter Parker";
students[0] = studentName;
studentName = null;
...
</code></pre>

<p>My answer was studentName is eligible for garbage Collection.But the answer given was both are not eligible.What I thought was students[0] refers to the String "Peter Parker" and studentName also does the same.Now that studentName refers to null, students[0] remains referring to "Peter Parker" ( I checked this by printing it out).The explanation given was students[0] is still referring to studentName so studentName is also not eligible for garbage Collection.But I'm not understanding this that since studentName now refers to null and students[0] refers to "Peter Parker".Is my understanding wrong?</p>