3

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.

2
  • 1
    You may be confused by the fact that there are two objects, but multiple references to each. studentName is not an object, it's a variable, a reference. Same for students. Similarily, students[0], the first element of the array students refers to, is not a string object but a reference to a string. Commented Feb 7, 2013 at 17:17
  • Ah - think I understand now....thanks for the prompt response. Commented Feb 8, 2013 at 0:28

3 Answers 3

10

Before assigning null to studentName there are two references to "Peter Smith" (studentName and students[0]). After null is assigned to studentName, "Peter Smith" is still referenced by students[0]

Sign up to request clarification or add additional context in comments.

4 Comments

So studentName is not the "String object" that the question refers to, but "Peter Smith" itself is? I.e. studentName not an object? Have I simply misunderstood the question? thanks.
studentName is an object variable, and as such can hold a reference to an object. Such variables are commonly referred to as "objects" in various contexts, and it only causes confusion in limited circumstances such as this one. The question states that the code creates one array and one string object. The object itself is "Peter Smith"; studentName is one place where it is (momentarily) referenced. The object is still referenced from the 0th element of the array at the end of these four lines.
I see. I think my misunderstanding was deeper than I thought. So the code = "Peter Smith" effectively initalizes a new string object with the value "Peter Smith", and because the string array has this object referenced from its 0th element, it remains. In turn the "Peter Smith" string object stays because it is referenced by this array? Verbose I know, but I just want to be sure I understand properly.
Ah. Thanks for all your prompt responses - really helpful.
1

What are the objects, actually, that cannot be garbage collected? "Peter Smith" and the String[10] array. Note these are the objects themselves, not the references to the objects! It is enough that just one reference exists to avoid garbage collection.

I use a mental trick which is to be verbose while reading code. It is easy to say "String" (vague) when what you mean is "String object" (the words in the String) or "String reference" (the variable pointing to the words) so this trick avoids this confusion.

Rewriting the pseudocode with my mental trick, labeling everything as object or reference the way I do in my mind:

1 String[] OBJECT student REFERENCE = new String[10] OBJECT;
2 String OBJECT studentName REFERENCE = "Peter Smith" OBJECT;
3 students[0] REFERENCE = studentName REFERENCE;
4 studentName REFERENCE = null;

In line 3 the String[0]_reference was pointed at the "Peter Smith" object. It is now clear that just the studentName reference was set to null but the "Peter Smith" object continues to exist and the array still points to it. Thus the objects aren't ever null, just one reference was nulled.

Comments

1

This page explains the fundamental java concept of references well (and how they are essentially just pointers)

http://javadude.com/articles/passbyvalue.htm

Though not about the question, it is directly relevant. Objects are referenced by pointers (references) in java. Understanding that should help a lot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.