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();
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Mrak Vladar
  • 598
  • 4
  • 18
  • 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. – Alexandre Dupriez Dec 28 '18 at 13:11
  • a `Dog[]` is capable of *holding* `Dog` references; `pets = new Dog[7];` holds no references yet. – Andrew Tobilko Dec 28 '18 at 13:23

8 Answers8

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.

Ricola
  • 2,621
  • 12
  • 22
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.

AntoineB
  • 4,535
  • 5
  • 28
  • 61
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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
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.

Sagar P. Ghagare
  • 542
  • 2
  • 12
  • 25
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

Tarun
  • 986
  • 6
  • 19
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

payloc91
  • 3,724
  • 1
  • 17
  • 45
  • You don't always need a new keyword for an array. check this link https://stackoverflow.com/questions/53227778/ways-of-using-array-in-java – Mrak Vladar Dec 28 '18 at 13:25
  • @MrakVladar that is just a shorthand. Under the hoods, the memory is till initialized with `new` – payloc91 Dec 28 '18 at 13:27
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.

Dacar
  • 21
  • 3
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).

Nisarg Patil
  • 1,509
  • 1
  • 16
  • 27