Taken from the official Java tutorial by Oracle, see question 2 here (boilerplate by me).
public static void main(String[] args) {
String[] students = new String[10];
String studentName = "Peter Smith";
students[0] = studentName;
studentName = null;
System.out.println(students[0]);
}
The answer says that studentName
is not eligible for garbage collection since the array students
still references it. However, the final line prints "Peter Smith", so to me it seems wrong that students[0]
references studentName
. Can someone please explain this?