Oracle Java tutorial - possible error at answer to question
Oracle Java tutorial - possible error at answer to question
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
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.
studentName
students[0]
I did not find an email address to report to. this is the second issue, I've found. see also stackoverflow.com/questions/51634863/character-autoboxing-java
– Eliyahu M
Aug 30 at 11:44
You could submit them through the main Java Bugs Database: 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 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: softwareengineering.stackexchange.com/questions/377197/… about this that explains how the JVM knows if something is garbage. I hope it helps.
– JimmyJames
Aug 30 at 20:23
3 Answers
3
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.
students
students
That sentence makes sense with @FarazDurrani answer
– XtremeBaumer
Aug 30 at 11:32
I don't see how @XtremeBaumer
– Max Vollmer
Aug 30 at 11:33
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
.
array
studentName
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.
studentName
null
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.
String
studentName
studentName
null
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 :
List
String a = "Peter";
List<String> list = ...
list.add(a);
a = null;
No object is eligible to be GC for the same reason.
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".
I asked about the 'students' array, when it is eligible to collection. how immutable string related to this?
– Eliyahu M
Aug 30 at 11:36
"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 at 16:37
String
ArrayList
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 at 19:31
String
"Peter Smith"
String
@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 at 20:42
students
@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 at 21:00
java.lang.Integer
int
ArrayList students = new ArrayList[10]; ArrayList studentName = new ArrayList(); students[0] = studentName; studentName = null;
students -> ArrayList instance
String
String
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Yes, it is a bug in the answer. If you care, report it :-)
– Stephen C
Aug 30 at 11:32