7.4.1 Creating and Initializing an Array

The application of Fig. 7.2 uses keyword new to create an array of 10 int elements, which are initially zero (the default initial value for int variables). Line 9 declares array—a reference capable of referring to an array of int elements—then initializes the variable with a reference to an array object containing 10 int elements. Line 11 outputs the column headings. The first column contains the index (0–9) of each array element, and the second column contains the default initial value (0) of each array element.

 1   // Fig. 7.2: InitArray.java 2   // Initializing the elements of an array to default values of zero. 3  4   public class InitArray 5   { 6      public static ...

Get Java™ How To Program (Early Objects), Tenth 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.