8.1 Declaring and Instantiating Arrays

In Java, arrays are implemented as objects, so creating an array takes two steps:

  1. Declaring the object reference for the array.

  2. Instantiating the array.

In arrays of primitive types, each element in the array contains a value of that type. For example, in an array of doubles, each element contains a double value. In arrays of objects, each element is an object reference, which stores the location of an object.

8.1.1 Declaring Arrays

To declare an array, we specify the name of the array and the data type, as we would for any other variable. Adding an empty set of brackets ([ ]) indicates that the variable is an array.

Here is the syntax for declaring an array:

datatype [ ] arrayName;

For example, ...

Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.