[java] The object array concept is confusing.

Asked 2 years ago, Updated 2 years ago, 30 views

Which of the following statements about the code is incorrect?

[ Book[] book = new Book[10]; ]

A book is a reference to an array.

Ten Book objects are created.

Only when you create objects with for(inti=0; i<book.size; i++) book[i] = newBook(); can the array be completed.

book[0], book[1], ..., book[9] are all references to Book objects.

I'm solving the above question, and I thought the answer was number 2. Book[] book = new Book[10]; With one line of code, only 10 reference variables containing Book objects are created, so I thought 10 Book objects were created and the correct answer was wrong The answer is 4

But number 4 is book[0]... Isn't that the correct expression because etc. is a reflection variable that contains the Book object address?

java

2022-09-22 18:48

1 Answers

The test seems to be asking if you know that you have allocated only sub-column space, not created real objects.

Of course, since you did not create a book object, the array elements are not reference values for the book object, that is, the object is created in heap through newBook(), and the heap address must be substituted into the array to assign a reference for the book object to the elements in the array.

But number two is also wrong.

Ten spaces are created to store references to Book objects.

I think it's very clear.


2022-09-22 18:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.