10 Mar 1996

Arrays

An array is an object of an unnamed class defined implicitly by the Java language itself and which has certain "magic" properties.

The elements of the array have the same type. However, if they are reference variables, then they can be assigned values that designate not only object of the named class, but also any subclass created by extending it. In the extreme case, an array of references to the Object class can contain values designating any dynamically allocated object of any predefined or user-defined class.

An array is an object. Objects do not have names. Objects are not stored within a variable. Objects cannot be defined as a field, argument, or local variable. Objects can only be created by the new operation, which yields a reference value that can be stored in a reference variable.

A reference variable with name "hours" that can designate an array object containing integer elements can be declared in two ways:

int [] hours;
int hours[];

The second form probably seems more natural to someone familiar with other languages, but it is likely to be misleading. It implies that "hours" is the name of the array. Although programmers may casually talk about "the array hours," this is bad language that will probably cause more harm than good. The name "hours" applies to a (scalar) reference variable whose value designates an object that is the array of integers. In the first statement "int []" appears where a type or class name would normally appear and more properly reflects the fact that "array of integers" is acting as an unnamed class.

Since the array is an object, it can only be created dynamically through the new operation. Since the array class is unnamed, the new operation recognizes the array nature of the object by the presence of the "[ ]" operator following a type or class name and enclosing a positive integer that is the length of the array. This statement dynamically allocates an array object with seven int elements, then assigns the reference value for the array to the reference variable "hours".

The index method can then be applied to the reference variable hours to yield an individual array element.

hours[3]=5;
if (hours[j]>8) {..}

The indexed array reference can appear both as an expression yielding a value and as the target of an assignment statement. As with C, the index of the first element of an array is 0. Indexing with a value less than 0 or beyond the length of the array throws an exception.

If "hours" is used in an expression that includes an index operation, then the value of the result is an element variable containing a value of the element type (in this case, an int). If "hours" is used without an index expression, then it yields a reference variable designating the entire array object.

Arrays of Arrays

Because an array is an object, and an array element can be a reference, it is possible to build an array of arrays. Well, that is what most people call it.

int [] [] worked = new int [12][];

This statement declares "worked" to be:

a (scalar) reference variable
whose value designates an array object
each of whose elements are reference variables
each designating an array object of integer elements.

The new operation then allocates:

an array object with 12 elements
each element is a reference variable
that will designate an as yet unallocated and undimensioned
array object with integer elements.

The twelve reference variables in the array are not explicitly initialized, so their value defaults to null. They can then be assigned reference values to new dynamically allocated integer array objects.

worked[0]=new int[31]; /*Jan*/
worked[2]=new int[29]; /*Feb*/
worked[3]=new int[31]; /*Mar*/
worked[4]=new int[30]; /*Apr*/

Each array object has a length (dimension) equal to the number of days in the corresponding month. The resulting structure can be regarded as a "table" with jagged rows.

Length (dimension) is an attribute of the individual object. It is not an attribute of the unnamed class of integer arrays, and it is not an attribute of any reference variable to the array. Thus all reference variables in the "worked" array of references have the same type even though the array objects that they designate have various lengths.

Index Order

Following the C language convention, Java evaluates subscripts from left to right. Therefore "worked[5][25]" corresponds to May 25. If this needs to be justified, imagine the operation grouped as "(worked[5])[25]". That is, the array object designated by "worked" is indexed by 5 to produce a reference value. The object designated by that value is then indexed by 25 to produce an int value.

Continue Back PCLT

Copyright 1996 PC Lube and Tune -- Java H. Gilbert