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?
[code=java]
...
String[] students = new String[10];
String studentName = "Peter Parker";
students[0] = studentName;
studentName = null;
...
[/code]
Answer 2: There is one reference to the students array and that array has one reference to the string Peter Parker. Neither object is eligible for garbage collection.
Q:- A program can set all references to an object to null so that it becomes eligible for garbage collection. Right?....But here we have set studentName to 'null' then why in answer its written "Neither object is eligible for garbage collection. "????
[code=java]
...
String[] students = new String[10];
String studentName = "Peter Parker";
students[0] = studentName;
studentName = null;
...
[/code]
Answer 2: There is one reference to the students array and that array has one reference to the string Peter Parker. Neither object is eligible for garbage collection.
Q:- A program can set all references to an object to null so that it becomes eligible for garbage collection. Right?....But here we have set studentName to 'null' then why in answer its written "Neither object is eligible for garbage collection. "????
Comment