6

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?

14
  • 2
    Are you saying you think printing something affects whether or not it is referenced? If students[0] didn't reference the string assigned to studentName, how is it being printed? Commented Feb 1, 2016 at 17:42
  • 3
    students[0] = studentName is creating a reference to the variable studentName, it is not setting students[0] to the string literal "Peter Smith". Commented Feb 1, 2016 at 17:42
  • 1
    Possible duplicate of Java Object Assignment Commented Feb 1, 2016 at 17:45
  • 1
    This may interest you javaranch.com/campfire/StoryPassBy.jsp Commented Feb 1, 2016 at 17:46
  • 1
    @Taelsin: "...is creating reference to the variable..." is wrong; it creates a reference to the same thing that that variable referenced at the time of assignment, which is why you can change what is in the variable and still reach that old value via the array element. Commented Feb 1, 2016 at 17:53

4 Answers 4

7

I would like to explain with the help of diagrams step by step.

String studentName = "Peter Smith";

After first line, a String is created in the String pool and a reference name studentName is pointing towards it.

Part1

students[0] = studentName;

At this point, another reference students[0] is pointing to the same object as shown in the below diagram. Now 2 references are pointing to the same object created in memory.

Part2

studentName = null;

After this point, the reference of studentName will point to null i.e. it is pointing to no object. But reference of students[0] is still pointing to the String object (Peter Smith) created.

enter image description here

Clearly after the third statement, one reference is pointing to null but one reference is still pointing to the Object created. Therefore the object is not eligible for garbage collection.

Hope it helps.

Update : Sorry if I have messed up with the variable names. But I hope you get the idea.

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

2 Comments

Thanks, great answer! Appreciate it. Erick's answer was spot on (and cleared up some confusion as to the wording in the tutorial), but I'm upvoting this too.
@cmeeren: Glad I could help..:)
5

You're confusing the referencing variables (studentName and students[0]) with the object that they reference ("Peter Smith"). studentName and students[0] both reference the String object "Peter Smith". Once neither of them reference "Peter Smith"--and assuming that there are no other references elsewhere--that object will be eligible for garbage collection. The references themselves go away when they go out of scope.

The tutorial got it wrong. There is no object studentName. studentName is a variable that, at one point, holds a reference to "Peter Smith" and at another point is null. Similarly, students[0] is one element in an array of memory locations that holds a reference to "Peter Smith" (the same String object). The tutorial confuses the idea of variables with the objects they reference.

I might speculate that they do this to avoid going into the details of memory usage. That's something of an advanced topic. But their explanation IMO causes more confusion than it prevents.

2 Comments

I did indeed get this wrong. However, the tutorial answer says "The object studentName is not eligible either because students[0] still refers to it." But isn't this wrong? Doesn't students[0] point to the string "Peter Smith", and not studentName? The tutorial answer seems to me to be mixing this up too.
I don't blame you for being confused. The answer to that question (Oracle tutorial #2) is poorly worded.
1

As far as I know, your code runs like this:

  1. String[] students = new String[10]; - Create students array. The students variable is now a pointer to the object in memory.
  2. String studentName = "Peter Smith"; - Assign the variable studentName the pointer to the value "Peter Smith". The studentName variable does not actually contain the name value.
  3. students[0] = studentName; - Assign the first element in the array the pointer to the the value "Peter Smith".

At this point both studentName and students[0] are pointing to the same thing, but neither of them contain the value itself.

  1. studentName = null; - this sets the reference to null. It does not clear the underlying memory allocation.
  2. System.out.println(students[0]); - This will print Peter Smith. The 0 element in the array contains a pointer to the original string object.

Because the array still contains a reference to the "Peter Smith" object, even though the original pointer (studentName) has been set to null, the object will not be eligible for garbage collection.


If you want to read a bit more about the way Java handles values and pointers these posts may help:

Comments

1

Think that there is an object pointing a memory area which has "Peter Smith" in it .when another object assigned to this object this second object is also pointing this memory cell.after that someone wants first object not to point any memory cell,but second one still points the same cell which still has the "Peter Smith" value in it so this is why last line prints the name. I think it is clear that jvm can not delete first object since it has still value, also even though second object has no pointer address someone else is still using it.(it is not the case if the seconds one is pointing an address or not) it is still in use by some one else

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.