Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Java tutorial - possible error at answer to question

Tags:

java

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.

like image 953
Eliyahu Machluf Avatar asked Aug 30 '18 11:08

Eliyahu Machluf


People also ask

What exception types can be caught by the following handler?

Answer: This handler catches exceptions of type Exception ; therefore, it catches any exception.

How does a program destroy an object that it creates Java?

Question: How does a program destroy an object that it creates? Answer: A program does not explicitly destroy objects. A program can set all references to an object to null so that it becomes eligible for garbage collection.

Which of the following is not a legal comment in Java?

Answer 1: Bytecode. Answer 2: c is an invalid comment.

What statement will execute the remaining code no matter the end result?

A try statement does not have to have a catch statement if it has a finally statement. If the code in the try statement has multiple exit points and no associated catch clauses, the code in the finally statement is executed no matter how the try block is exited.


2 Answers

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.

like image 189
Max Vollmer Avatar answered Oct 11 '22 08:10

Max Vollmer


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.

like image 21
davidxxx Avatar answered Oct 11 '22 09:10

davidxxx