Witscale Test Center

7.4 Arrays > 7.4.3 Array initialization


7.4.3 Array initialization

Un-initialized variables cause a lot of problem in many programming languages. Java takes care of this problem by making sure that every variable is initialized before its use. An array is no exception. When you create an array, it is automatically initialized at the time of its construction (at runtime). At that time, each element of array is initialized to a default value. Table 7.5  shows the default values for the array elements

 

Table 7.5 Default values for initialization of for Array elements

 

Array Element Type

Default Value

Object reference

null

byte

0

short

0

int

0

long

0L

float

0.0f

double

0.0d

boolean

false

char

‘\u0000’

 

 

In the next chapter we will learn about initialization of member variables with default values. For now remember that the default values depend on the datatype and they are same for array elements and the member variables. Note that the array elements can be either primitives or object references. The initialization is different in both these cases.

Array of primitive data type

Array of primitives may be created by specifying the primitive data type and the size of the array. For example, a character array is declared and constructed as:

 

 char[] vowels = new char[5];

 

At runtime, an array of five chars will be created and each element is automatically initialized to a null character,'\u0000'. Note that a primitive array can accept values that can be promoted implicitly to the declared type of the array. In Java, the narrower datatypes can be stored in a wider datatypes, but in the process, the narrower datatype is automatically converted to a wider datatype. For example, elements of type char can be stored in an array of type int[]. The conversions are covered in chapter 10, Conversion and Casting.

Array of object references

An array of object references type is created by specifying the object type and the size of the array. For example, an array of Date objects is declared and constructed as:

 

Date[] birthDates = new Date[5];

 

An array of five Date object references will be created with each element initialized to null. Note that though we have written the code where the birthDates array is declared, constructed and initialized in one line, only the declaration of birthDates will be done at compile time. The actual construction of the array object and initialization will be done only at runtime. Figure 7.7 elaborates the declaration and construction of an array of Date objects. The stack stores the reference variable, birthDates.

 


7.7 Array of object references

 


An array reference variable, birthDates is created and stored on the stack.

An array of Date object references of size 5 is created on the heap.

Each array element is initialized to null.

The reference of an array object on heap is stored in birthDates.


A date object is created for date August 21,1975. Its reference is stored as first element of array.


A date object is created for date September 2, 1997. Its reference is stored as second element of array.

 

 

There will be three objects on heap after the execution of this code, an array object and two date objects.

 

You may be asked questions based on object creation such as how many objects are created after executing a piece of code etc. For answering these types of questions, you need to understand the object creation process.

 

Explicit initialization with array initializer

We saw earlier how an array is constructed with a new keyword. Arrays can also be created with an array initializer. An array initializer is a code block with a comma-separated list of array elements, enclosed by a pair of curly braces. For example, you can create an array of strings as:

 

String[] names = {“Mickey”,”Pluto”,”Donald”};

 

The strings in the array initializer are the array elements to be placed in the newly created array. Note the curly brackets and semicolon. The sequence of the events when the above code is executed is as follows:

1.       A names array reference variable is created and stored on the stack.

2.       An array of String object references of size 3 is created on heap.

3.       Three string objects are created on heap.

4.       The first element is initialized with the reference to String object “Mickey”, second is initialized with reference to String object “Pluto” and third element is initialized with reference to String object “Donald”.

5.       The reference of an array of String object references is stored in names.

 

When using the initializers, an array must be declared, constructed and explicitly initialized at the same time. For example,

 

int[] numbers;             // array declaration.  

numbers = {7, 5, 3, 11};  // error!

 

In the above code, numbers array is declared first. However, when it is initialized with the initializer block, compiler error occurs saying, “Array constants can only be used in initializers”.