2

I'm new to java, I was reading Head first Java when I came across object in arrays. The code is from the book itself, I get the flow of the code but I don't really understand the second line. What does new Dog[7] do, in the book it says

"Declare and Create a Dog array to hold 7 Dog references"

If we have already created dog references why do we need to create a dog reference for individual array items again.

Dog[] pets;
pets = new Dog[7];
pets[0] = new Dog();
2
  • 3
    new Dog[7] creates an empty array of Dog, that is [null, null, null, null, null, null, null]. You still need to assign an actual instance for each index of the array. Commented Dec 28, 2018 at 13:11
  • a Dog[] is capable of holding Dog references; pets = new Dog[7]; holds no references yet. Commented Dec 28, 2018 at 13:23

8 Answers 8

2

There is a difference between declaration and initialization.
Dog[] pets declares that the variable pets is an array of Dogs
pets = new Dog[7] initializes the variable pets, it assigns it a value. The value is an array of size 7 full of null references.

It is the same for primitives :

int i; //declaration
i = 5; //initialization

As well as you can write

int i = 5;

you can write

Dog[] pets = new Dog[7];

In this case, you do the declaration and initialization on the same line.

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

Comments

1

Before the second line, you just say that the variable pets exists, and it's an array of Dog, but the array doesn't exist in memory, because it hasn't been created.

In order to be able to use the array and read / store values inside, you need to actually create this array in memory, which is what the 2nd line does: it creates an array of Dog, of size 7.

Comments

1

The first line declares a variable of a "Dog array" type. The second line actually initializes it with an array that has seven slots, each of which are null. The third line creates an actual Dog instance, and assigns it to the first slot of the array.

Comments

1
Dog[] pets;

This will declare an array of dog. But when you do pets = new Dog[7]; it will initialize array with length of 7.

pets[0] = new Dog();

this statement will store an object of dog at 0th position.

Comments

1

This line pets = new Dog[7]; creates a Array Object which will contain Dog objects.
So initially all 7 indeces in pets array are null. Therefore pets[0] = new Dog(); required to create Dog Objects

Comments

0

With that you are declaring and creating a java array able to contain 7 items of dog type. From a machine stand point you are creating a pointer to the memory space to contain your objects.

Comments

0

Dog[] pets; : decalres the array of Dog
pets = new Dog[7]; : Creates an array (with contiguous memory allocation on heap) of Dog to hold 7 Dog references (initially null).

Comments

0

Every object in must be initialized with the keyword new, which allocates memory on the heap.

Arrays in java are also objects, thus they must be initialized with new.

By default, every object in the array will have a null value. So you must initialize every individual item with the new keyword again.

Situation at each step:

 Dog[] pets;            // <-- reference to an array of Dog, still uninitialized
 pets = new Dog[7];     // <-- now pets points to an array of 7 Dog's. Array value is [null, null, null, null, null, null, null]
 pets[0] = new Dog();   // <-- array now is [Dog@some_memory_address, null, null, null, null, null, null]

See the oracle tutorial for arrays

2 Comments

You don't always need a new keyword for an array. check this link stackoverflow.com/questions/53227778/…
@MrakVladar that is just a shorthand. Under the hoods, the memory is till initialized with new

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.