next up previous
Next: 2.1.1.1 Recipes for Processing Up: 2.1 Sequences Previous: 2.1 Sequences

  
2.1.1 Arrays

An array is an indexed sequence d0, d1, d2, ..., dn, n>=0 of data values of fixed length. In contrast to more general representations for sequences an array object cannot grow or shrink. It has a specific length n>=0 when it is created and retains this length for as long as it exists. Java arrays are almost identical Scheme vectors; the only difference is that every array in Java has a declared type T[] asserting that all elements of the array have type T. All of the primitive operations on arrays Java preserve the declared types attached to arrays.

In Java arrays of every type are built into the language. If you define a class C, then the array type C[] is automatically supported by Java. Every array type is a subtype of type Object, but array types cannot be extended. Array indexing at 0 as in Scheme, C, and C++. Hence an array length n has valid indices of 0, 1, ..., n-1. In essence, Java arrays are identical Scheme vectors augmented by an enforced type constraint. Every Java array has a int field length that contains the length of the array. If a Java variable has type T[] for some primitive or object type T then it can only be bound to arrays containing elements of type T.

A Java array value is actually a reference to an array. Hence, a variable of type T[] in Java can have the value null. In addition, two variables can be bound to exactly the same array. Scheme variables bound to vectors have precisely the same properties.

Arrays are allocated in Java using the new statement just like other objects. The array form is

new T[length]
where T is any type and length is an expression that evaluates to a non-negative int. Arrays can have length 0. Each element of the array is set to the `zero'' value of the declared type T. If T is an object type, the initial value of each element is null. Hence, the expression
new String[1000]
allocates an array of 1000 String elements all bound to null.

Java has alternate form for constructing an array called an anonymous array. The expression

new T[] { v0, ... vn}
allocates an array of length n of type T containing the specified sequence of elements.

Java uses two mechanisms to enforce the declared type T[] of an array object. First, when an array T[] is allocated, all of the initial element values must be of type T. Second, when an array object of type T[] is updated by an assignment to an element of the array, the new value stored in the array must be of type T.

Java array variables have an interesting property called co-variance: an array variable of type T[] can be bound to an array A of type T'[] provided $T' \subseteq T$. As a result, the array type Object[] is a supertype for all object array types, which permits arrays to be treated polymorhically in some situations.



 
next up previous
Next: 2.1.1.1 Recipes for Processing Up: 2.1 Sequences Previous: 2.1 Sequences
Corky Cartwright
2000-01-07