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?



students[0]didn't reference the string assigned tostudentName, how is it being printed?students[0] = studentNameis creating a reference to the variablestudentName, it is not settingstudents[0]to the string literal"Peter Smith".