18

I'm new to Java, reading Oracle tutorial. After each section, there are questions and answers, and I don't understand a sentence within one answer (see below bolded line).

source is https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html

I'm referring to question 2, see the bolded words. As far as I understand, an array is eligible to garbage collection, if there is no reference to the array. It does not matter, whether there is a reference to the objects held by this array, as the inner objects (within the array) have their own reference counting. Is that right? Please explain the bolded sentence.

cite from oracle tutorial: https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html

  1. Question: 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. The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null. The object studentName is not eligible either because students[0] still refers to it.

Hulk
  • 6,399
  • 1
  • 30
  • 52
Eliyahu Machluf
  • 1,251
  • 8
  • 17
  • 3
    Yes, it is a bug in the answer. If you care, report it :-) – Stephen C Aug 30 '18 at 11:32
  • I did not find an email address to report to. this is the second issue, I've found. see also https://stackoverflow.com/questions/51634863/character-autoboxing-java – Eliyahu Machluf Aug 30 '18 at 11:44
  • 3
    You could submit them through the main Java Bugs Database: https://bugs.java.com/. I can see that other people have been doing that. (But it may be a long time before they are fixed. The last major updates to the Tutorials were in 2016.) – Stephen C Aug 30 '18 at 12:01
  • "as the inner objects (within the array) have their own reference counting. Is that right?" Just as an aside, Java doesn't use reference counting. I wrote an answer here: https://softwareengineering.stackexchange.com/questions/377197/how-a-garbage-collector-follows-pointers-to-discover-live-objects/377231#377231 about this that explains how the JVM knows if something is garbage. I hope it helps. – JimmyJames Aug 30 '18 at 20:23

3 Answers3

15

The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null.

Yeah, that sentence is... odd. It makes no sense.

An array can be eligible for garbage collection, no matter what references it holds to other objects.

students is a reference to the array, so it's not eligible for garbage collection as long as students remains in scope.

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
14

Neither object is eligible for garbage collection.

It is right.

But the explanation is unclear :

The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null.

studentName is not an object, it is a variable.
Besides, null elements in the array will not have influence on the array eligibility to be GC but it will have only on the GC eligibility of the array elements.

For example :

String[] students = new String[10];
// the object referenced by students is not eligible to be GC

Or :

String[] students = new String[10];
String studentName = "Peter Smith";
students[0] = studentName;
students[0] = null;
// no object is eligible to be GC 

A correct sentence could be :

The String object is not eligible for garbage collection because the object previously referenced by the studentName variable is still referenced by the array and assigning a new object to a variable (as assigned studentName to null) changes only the reference of this variable, not these of variables that refer the same object.


Note that the array doesn't change nothing in the way which Java works with object assignment.
With a List you could notice the same behavior.
For example :

String a = "Peter";
List<String> list = ...
list.add(a);
a = null;

No object is eligible to be GC for the same reason.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
-4

It all has to do with Strings being immutable, meaning once created, they cannot be changed. So when you do this,

String studentName = "Peter Smith";

and then you do this,

studentName = null,

studnentName now points to another memory address that points to null. "Peter Smith" is still in the memory somewhere.

After a value "Peter Smith" is assigned to student[0], student[0] still holds that value even after setting studentName to null. Because student[0] holds a reference to a place in memory that holds "Peter Smith".

Faraz
  • 6,025
  • 5
  • 31
  • 88
  • 5
    I asked about the 'students' array, when it is eligible to collection. how immutable string related to this? – Eliyahu Machluf Aug 30 '18 at 11:36
  • 1
    *"It all has to do with Strings being immutable"*, nope. Replacing the type `String` with `ArrayList` wouldn't change a thing for the garbage collector. You're probably refering to the fact that java is pass by value, so in this case assigning a new value to the variable of a non-primitive type does not change other references. – fabian Aug 30 '18 at 16:37
  • 1
    The fact that `String` is immutable is 100% irrelevant to this example. Now, the behavior of whether or not `"Peter Smith"` is garbage collected *is* different from other types of objects, due to *`String` interning*, which is a separate concept from immutability. (But this is, as already noted, irrelevant to the question, which is about the array.) – Radiodef Aug 30 '18 at 19:31
  • 1
    @FarazDurrani what an array contains is entirely irrelevant to whether the array is eligible for garbage collection. It only matters whether or not *other* things have a reference to it (in this case, `students` is that reference) – mbrig Aug 30 '18 at 20:42
  • @FarazDurrani You do notice `java.lang.Integer` isn't mutable either? If you're referring to primitive `int`: they're going to the stack, so talking about them being garbage collected is pointless (of course for arrays the gc is still involved). But if you rewrite the code from the tutorial as `ArrayList[] students = new ArrayList[10]; ArrayList studentName = new ArrayList(); students[0] = studentName; studentName = null;` there would still be the reference chain `students -> ArrayList instance`. (In fact `String`s are a bit more difficult to reason about because of the `String` pool.) – fabian Aug 30 '18 at 21:00