Contents | Prev | Next | The T Language Specification Spring 2005 |
CHAPTER 6
In this language, arrays are objects that are dynamically created, and may be assigned to variables of type Object
.
An array object contains a number of variables. The number of variables may be zero, in which case the array is said to be empty. The variables contained in an array have no names; instead they are referenced by array access expressions that use nonnegative integer index values. These variables are called the components of the array. If an array has n components, we say n is the length of the array; the components of the array are referenced using integer indices from 0 to n - 1, inclusive.
All the components of an array have the same type, called the component type of the array. If the component type of an array is T, then the type of the array itself is written T[]
.
The component type of an array may itself be an array type. The components of such an array may contain references to subarrays. If, starting from any array type, one considers its component type, and then (if that is also an array type) the component type of that type, and so on, eventually one must reach a component type that is not an array type; this is called the element type of the original array, and the components at this level of the data structure are called the elements of the original array.
There are some situations in which an element of an array can be an array: if the element type is Object
, then some or all of the elements may be arrays, because any array object can be assigned to any variable of this type.
An array type is written as the name of an element type followed by some number of empty pairs of square brackets []
. The number of bracket pairs indicates the depth of array nesting. An array's length is not part of its type.
The element type of an array may be any type, whether primitive or reference. Array types are used in declarations.
A variable of array type holds a reference to an object. Declaring a variable of array type does not create an array object or allocate any space for array components. It creates only the variable itself, which can contain a reference to an array.
Because an array's length is not part of its type, a single variable of array type may contain references to arrays of different lengths.
The []
may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example:
int[] rowvector, colvector, matrix[];
This declaration is equivalent to:
int rowvector[], colvector[], matrix[][];
Once an array object is created, its length never changes. To make an array variable refer to an array of different length, a reference to a different array must be assigned to the variable.
If an array variable v has type A[]
, where A is a reference type, then v can hold a reference to an instance of any array type B[]
, provided B can be assigned to A. This may result in a run-time error on a later assignment; see §6.7 .
An array is created by an array creation expression (§8.8).
An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting. The array's length is available as an instance variable length
. However, all references to the length
instance variable
denotes its value. (That is, the length is set at the time the array is
created and may not be later modified.)
A component of an array is accessed by an array access expression (§8.11) that
consists of an expression whose value is an array reference followed by an indexing expression enclosed by [
and ]
, as in A[i]
. All arrays are 0-origin. An array with length n can be indexed by the integers 0 to n-1.
Arrays must be indexed by int
values. All array accesses are checked at run time; an attempt to use an index that is less than zero or greater than or equal to the length of the array causes a run-time error message such as
"array index out of bounds at line x" to be displayed. The program is halted
after the error message is displayed. 'x' is the line number at which the
error occurred.
There is a single field defined for the array members:
The field length
, which contains the number of components
of the array (length
may be positive or zero).
All references to the length
field denotes its value.
Every array has a run-time type, shared with all other arrays with the
same component type. The direct superclass of an array type is
Object
.
If an array variable v has type A[]
, where A is a reference type, then v can hold a reference to an instance of any array type B[]
, provided B can be assigned to A.
An assignment may seem valid at compile time, but, it may happen that at run-time, the actual object to which the array references may not be assignment compatible. In this case, an error message "Run-time assignment for array element invalid at line x" is displayed and the program halts.
Thus a run-time type check is done to ensure that the assignment is valid;
if not, an error message is displayed. More formally: an assignment to an
element of an array whose type is A[]
, where A
is a reference type, is checked at run-time to ensure that the value assigned can be assigned to the actual element type of the array, where the actual element type may be any reference type that is assignable to A.
The delete
operation can be carried out for arrays too. This will
just free up the space allotted for the array. If the array elements are
references to objects, then the memory allocated for those objects will
not be freed by this operation. Users have to take care to free up the
memory for component objects before they free the memory allocated for
the array, in order to avoid memory leaks.
Contents | Prev | Next | The T Language Specification Spring 2005 |
Author(s): Shilpa Kulkarni (§6.1-6.8)