I was going through the online tutorial provided by oracle. One of the exercises has a question as follows:
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?
... String[] students = new String[10]; String studentName = "Peter Smith"; students[0] = studentName; studentName = null; ...Answer: There is one reference to the students array and that array has one reference to the string Peter Smith. Neither object is eligible for garbage collection.
(http://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html)
Surely the last line means studentName is eligible for GC? Really confused, and I think this means I have not understood the nature of "null" and also object referencing properly, which is why I ask.
studentNameis not an object, it's a variable, a reference. Same forstudents. Similarily,students[0], the first element of the arraystudentsrefers to, is not a string object but a reference to a string.