Skip to content

Creating

Declaring an array variable does not create the array. * It simply tells Java the variable will point to an array.

Declaring does not create

To store items in it we must instantiate the array.

instantiate

Make an instance of an object. This is a fancy way of saying "use new to create an object."

An array is an object so we use the new operator to create the array.

We must tell the compiler how many elements the array will contain.

int[] scores = new int[7];
Look at what each side of the = is doing.

Instantiating an array

Practice Exercise

The number of array slots never goes in the declaration.

int scores[7]; // WILL NOT COMPILE

Drill

Arrays/src/drills/ArrayCreate.java * Initialize the variable to hold 7 Strings * Initialize the variable to hold 6 chars. * Initialize the variable to have the correct number of double slots.


Prev -- Up -- Next