|
Like other Java objects, the array objects too only need be declared at compile-time. You can construct them later at runtime. The actual array construction with a new keyword involves the memory allocation and the array object creation at runtime. The Following example shows how a simple array is declared at compile time and constructed at runtime.
int[] intArray; // array declaration
intArray = new int[5]; // array construction at runtime.
At compile time, the reference variable intArray of type int[] (integer array) is created. At runtime, Java will create an array object of integers with five elements on memory heap and assign its reference to the variable intArray. Figure 7.5 shows the memory model after the integer array declaration and creation.
![]() |
Figure 7.5 Memory model after Array declaration and construction
|
|
The intArray variable is created and stored on the stack after the array declaration. |
|||
|
|
An array object of size 5 is created on the memory heap. |
|||
|
|
The reference of newly created array object is stored in the intArray.
|
You can also write the array declaration and creation on the same line. But the array will still be constructed at runtime. For instance, the following code will create the array object only at runtime.
int[] intArray = new int[5]; // array construction at runtime.
Furthermore, the pair of empty square brackets in the declaration of the reference variable intArray indicates that the intArray does not need to know about the size of the array to which it may refer. The variable intArray may refer to an integer array object of any size. In addition, it may refer to different integer array objects at different times during the execution of the program. Figure 7.6 shows intArray variable and how it can be assigned to the references to array objects of different sizes.

Figure 7.6 Reference variable and array objects
|
|
The intArray can refer only to the array objects of type int[]. It cannot reference to an array object holding incompatible data types, for example, array of String objects. Type compatibility is covered in detail in Chapter 10, Conversion and Casting |
When an array object is created, you need to specify how many elements it is going to hold. It is the array size. Since the array object itself is not created until the runtime, the array’s size is not required till that time. You can specify a variable or an expression as a value of the array’s size.. The only compulsion Java puts is that the array size should be calculated and known at the time of array construction (at runtime). For example, in the following code, the array size can be evaluated at compile time itself. It will be 20 at compile-time.
int population = 10;
int total = population * 2;
int[] intArray; // array declaration
intArray = new int[total]; // array construction at runtime.
When you run this code, the array object will be created of size 20. Its reference will be stored in variable intArray.
|
|
Once an array is created, its size cannot change. For instance, if you create an array object of size 10. It will always remain an array object of size 10. It cannot grow as the elements in it grow or even shrink. We will learn about Java collections (chapter 14)) such as Vector, Hashtable that can change their size dynamically. |