7
votes

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 Parker";
students[0] = studentName;
studentName = null;
...

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?

3
Can you give a link to the place that says that it refers to studentName rather than the string "Peter Parker"? - RealSkeptic
No string reference is eligible for garbage collection - nikesh joshi
I think you should change the title of this post to something that will help future visitors, search engines to help find this post. - Raman Sahasi
So here is the link for the explanation link The last sentence says the object studentName is also not eligible , so this is very confusing as studentName is a reference variable and it refers to null now. - getsuga

3 Answers

6
votes

Before studentName = null; is executed, studentName and students[0] both hold references to the same String object (whose value is "Peter Parker"). When you assign null to studentName, students[0] still refers to that object, so it can't be garbage collected.

Java doesn't garbage collect reference variables, it garbage collects the objects that were referenced by these variables once there are no more references to them.

3
votes

Here String object is created with "Peter Parker" and studentName is just referring it. Java will garbage collecting those object who are lost and have no reference , here you are confused with reference studentName. by assigning null to studentName you are just removing one reference of "Peter Parker"; but it still refer by array element , so it will not garbage collected.

3
votes

How many objects do you have?

You have two objects:

  • The string "Peter Parker"
  • An array of strings.

How many references are/were there to those objects?

There were three references:

  • The variable studentName is referencing the string object.
  • The variable students is referencing the array object.
  • The first position of the array object, students[0] is also referencing the string object.

How many references to those objects exist after the code executes?

After executing this line:

studentName = null;

Only the first of the three references has been deleted. We still have two references left.

Is either object eligible for garbage collection?

No, because:

  • The array object is still referenced by the students variable.
  • The string object "Peter parker" is still referenced by the first position of the array object, students[0].

Since both objects are still referenced somewhere, none of them are eligible for garbage collection.


PS: I've used generic "string" and "array" terms for the objects on purpose, so it's easier to mentally dissociate them from the actual String and String[] variables in the code.