This article illustrates three ways of initializing an array, using the primitive type, string type, and user-defined type. The string type and the user-defined types are reference types. The article begins with the initialization of the array with a primitive type.
Initializing Array with Primitive Type
The char type will be used here. char means character. Other primitive types are done in the same way. All the code in this section takes place in the main() method. The statement,
declares an array without any initial value and without the number of characters indicated. The number of characters for the array is the length of the array. Once the length has been decided upon, it remains that way until the end of the program. Another statement is needed, together with this one, to give length to this array, as follows:
arr = new char[5];
Now, the array’s number of characters (length) is 5. Some initialization has taken place with the second statement. However, this is not practical initialization. It is initialization with 5 default values for char. The default value for char is the empty character, i.e. ” , it has no space.
The next code segment assigns one practical value (character) to each of the memory locations, of the 5 character variables of the array:
arr[1] = 'G';
arr[2] = 'H';
arr[3] = 'I';
arr[4] = 'J';
This is an assignment or replacement. It is not initialization. Initialization was done with default values. The above two declaration statements can be done in one statement, as follows:
The name of the array is arr. char is the reserved word for char, which appears on both sides of the assignment operator. New is another operator. It creates the object with default values. The second square bracket in the whole statement has the length of the array. At this point, each element of the array still needs to be given a practical value. That is the second way of declaring an array.
The third way of declaring an array involves initialization with practical values. It is in two forms, as follows:
or
The first form has the array literal, which is called the array initializer. It is in braces. Each character is in a single quote. The array initializer indirectly determines the length of the array (number of elements). The first form does not have the new operator. The second form has the new operator. However, here, the second square brackets do not have the array’s length, as the length is implicit in the array initializer, coded next to it.
Initializing Array of Strings
The string object is a very good example of the reference type in Java. There are two kinds of types in Java: primitive and reference types. The primitive types are: boolean, byte, char, short, int, long, double, float. The following statement declares an array of strings without any length defined and any initial value (be it default or practical).
Another statement is needed, together with this one, to give length to this array, as follows:
Now the number of strings (references) of the array is 4. Some initialization has taken place with the second statement. However, this is not practical initialization. It is initialization with 4 default values for String. The default value for string is null, without quotes. The following code in the main() method illustrates this:
The output is:
The following code segment assigns one practical value (string reference) to each of the memory locations, of the 4 string variables of the array:
arr[1] = "two";
arr[2] = "three";
arr[3] = "four";
This is an assignment or replacement. It is not initialization. Initialization was done with default values. The above two declaration statements can be done in one statement, as follows:
The name of the array is arr. A string is a reserved word for string, which appears on both sides of the assignment operator. New is another operator. It creates the object array with default values. In the whole statement, the second square bracket has the length of the array. At this point, each element of the array still needs to be given a practical value. That was the second way of declaring an array.
The third way of declaring an array involves initialization with practical values. It is in two forms, as follows:
or
The first form has the array literal, which is called the array initializer. It is in braces. Each character is in a single quote. The array initializer indirectly determines the length of the array (number of elements). The first form does not have the new operator. The second form has the new operator. However, here, the second square bracket does not have the length of the array, as the length is implicit in the array initializer, coded next to it.
Note: To use the string or array in Java, neither the string class nor the array class has to be imported.
User-Defined Objects
The following is an example of a user defined class:
int prop;
int mthd() {
return prop;
}
}
Aclass is the name of the class from which its objects will be created.
In the main() function, the following code segments can be employed:
Each code segment creates an object of type, Aclass. Each is a user-defined object. Each code segment assigns an integer to the property (field) of each object. There are three different objects of the same class. Each code segment calls its method for its object. The output for the three code segments should be: 1 2 3, with each number in a separate line.
The declaration of an array of a user-defined class type is done in the same way as in the above cases. For example,
declares an array of three objects of type, Aclass. This statement created an array initialized with the default type value. For any reference type, including the string type, the default value is null. This means there are three values of null in the array, arr, now. The following code in main() should prove this:
The output is:
The following statement initializes the array with practical objects of the Aclass type:
The output is:
three different codes. The reason for this is that the array expects object literals, but object references have been given.
In the above procedure, the object names, obj1, obj2, and obj3, were created (instantiated) before writing them into the array. To work around this problem, instantiate the objects as array elements, without their names, as follows:
From here, instead of using the object names, obj1, obj2 obj3 to access the properties and methods, use the array indexes as follows:
The solves the problem. And so the above three code segments can be rewritten as:
And the ultimate output expected, is as before, that is: 1 2 3, with each number in its own line.
Conclusion
There are two kinds of types in Java: primitive types and reference types. An array can be declared without any element value and length. An array can be declared with its length but initialized to its default values by the compiler. An array can be declared, initialized with the practical values. If the values are references, and the literals cannot be used as values, then the array values should be the instantiations of the class.