<< Chapter < Page Chapter >> Page >
Figure 1 . Formats for declaring a reference variable for an array object.
int[] myArray;int myArray[];

Declaration does not allocate memory

As with other objects, the declaration of the reference variable does not allocate memory to contain the array data. Rather it simplyallocates memory to contain a reference to the array.

Allocating memory for the array object

Memory to contain the array object must be allocated from dynamic memory using statements such as those shown in Figure 2 .

Figure 2 . Allocating memory for the array object.
int[] myArrayX = new int[15]; int myArrayY[]= new int[25];int[] myArrayZ = {3,4,5};

The statements in Figure 2 simultaneously declare the reference variable and causememory to be allocated to contain the array.

Also note that the last statement in Figure 2 is different from the first two statements. This syntax not only sets aside thememory for the array object, the elements in the array are initialized by evaluating the expressions shown in the coma-separated list inside the curlybrackets.

On the other hand, the array elements in the first two statements in Figure 2 are automatically initialized with the default value for the type.

Declaration and allocation can be separated

It is not necessary to combine these two processes. You can execute one statement to declare the reference variable and anotherstatement to cause the array object to be instantiated some time later in the program as shown in Figure 3 .

Figure 3 . Declaration and instantiationcan be separated.
int[] myArray;. . . myArray = new int[25];

Causing memory to be set aside to contain the array object is commonly referred to as instantiating the array object (creating an instance of the array object) .

If you prefer to declare the reference variable and instantiate the array object atdifferent points in your program, you can use the syntax shown in Figure 3 . This pattern is very similar to the declaration and instantiation of all objects.

General syntax for combining declaration and instantiation

The general syntax for declaring and instantiating an array object is shown in Figure 4 .

Figure 4 . General syntax for combining declaration and instantiation.
typeOfElements[] nameOfRefVariable =new typeOfElements[sizeOfArray]

Accessing array elements

Having instantiated an array object, you can access the elements of the array using indexing syntax that is similar to many other programming languages. An example is shown in Figure 5 .

Figure 5 . An example of array indexing syntax.
myArray[5] = 6;myVar = myArray[5];

The value of the first index

Array indices always begin with 0.

The length property of an array

The code fragment in Figure 6 illustrates another interesting aspect of arrays. (Note the use of length in the conditional clause of the for loop.)

Figure 6 . The use of the length property in the conditional clause of a for loop.
for(int cnt = 0; cnt<myArray.length; cnt++) myArray[cnt]= cnt;

All array objects have a length property that can be accessed to determine the number of elements in the array. (The number of elements cannot change once the array object is instantiated.)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask