In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Show
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 non-negative 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 value of an array component of type 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 10.1. Array TypesArray types are used in declarations and in cast expressions (§15.16). An array type is written as the name of an element type followed by some number of empty pairs of square brackets An array's length is not part of its type. The element type of an array may be any type, whether primitive or reference. In particular:
The supertypes of an array type are specified in §4.10.3. The direct superclass of an array
type is Every array type implements the interfaces 10.2. Array VariablesA 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. However, the initializer part of a declarator (§8.3, §9.3, §14.4.1) may create an array, a reference to which then becomes the initial value of the variable. Example 10.2-1. Declarations of Array Variables int[] ai; // array of int short[][] as; // array of array of short short s, // scalar short aas[][]; // array of array of short Object[] ao, // array of Object otherAo; // array of Object Collection<?>[] ca; // array of Collection of unknown type The declarations above do not create array objects. The following are examples of declarations of array variables that do create array objects: Exception ae[] = new Exception[3]; Object aao[][] = new Exception[2][3]; int[] factorial = { 1, 1, 2, 6, 24, 120, 720, 5040 }; char ac[] = { 'n', 'o', 't', ' ', 'a', ' ', 'S', 't', 'r', 'i', 'n', 'g' }; String[] aas = { "array", "of", "String", }; The For example: byte[] rowvector, colvector, matrix[]; This declaration is equivalent to: byte rowvector[], colvector[], matrix[][]; In a variable declaration (§8.3, §8.4.1, §9.3, §14.14, §14.20) except for a variable arity parameter, the array type of a variable is denoted by the array type that appears at the beginning of the declaration, followed by any bracket pairs that follow the variable's Identifier in the declarator. For example, the local variable declaration: int a, b[], c[][]; is equivalent to the series of declarations: int a; int[] b; int[][] c; Brackets are allowed in declarators as a nod to the tradition of C and C++. The general rules for variable declaration, however, permit brackets to appear on both the type and in declarators, so that the local variable declaration: float[][] f[][], g[][][], h[]; // Yechh! is equivalent to the series of declarations: float[][][][] f; float[][][][][] g; float[][][] h; We do not recommend "mixed notation" in an array variable declaration, where brackets appear on both the type and in declarators. 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. A single variable of array type may contain references to arrays of different lengths, because an array's length is not part of its type. If an array variable 10.3. Array CreationAn array is created by an array creation expression (§15.10) or an array initializer (§10.6). 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 a An array initializer creates an array and provides initial values for all its components. 10.4. Array AccessA component of an array is accessed by an array access expression
(§15.13) that consists of an expression whose value is an array reference followed by an indexing expression enclosed by All arrays are Example 10.4-1. Array Access class Gauss { public static void main(String[] args) { int[] ia = new int[101]; for (int i = 0; i < ia.length; i++) ia[i] = i; int sum = 0; for (int e : ia) sum += e; System.out.println(sum); } } This program produces the output: 5050 The program declares a variable Arrays must be indexed by An attempt to access an array component with a 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
an 10.5. Array Store ExceptionFor an array whose type is A If the type of the value being assigned
is not assignment-compatible (§5.2) with the component type, an If the component type of an array were not reifiable (§4.7), the Java Virtual Machine could not perform the store check described in the preceding paragraph. This is why an array creation expression with a non-reifiable element type is forbidden (§15.10). One may declare a variable of an array type whose element type is non-reifiable, but assignment of the result of an array creation expression to the variable will necessarily cause an unchecked warning (§5.1.9). Example 10.5-1. class Point { int x, y; } class ColoredPoint extends Point { int color; } class Test { public static void main(String[] args) { ColoredPoint[] cpa = new ColoredPoint[10]; Point[] pa = cpa; System.out.println(pa[1] == null); try { pa[0] = new Point(); } catch (ArrayStoreException e) { System.out.println(e); } } } This program produces the output: true java.lang.ArrayStoreException: Point The variable A reference to this array On the other hand, an assignment to the array The Java Virtual Machine checks for such a situation at run time to ensure that the assignment is valid; if not, an 10.6. Array InitializersAn array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values. ArrayInitializer: VariableInitializers: The following is repeated from §8.3 to make the presentation here clearer:
An array initializer is written as a comma-separated list of expressions, enclosed by braces A trailing comma may appear after the last expression in an array initializer and is ignored. Each variable initializer must be assignment-compatible (§5.2) with the array's component type, or a compile-time error occurs. It is a compile-time error if the component type of the array being initialized is not reifiable (§4.7). The length of the array to be constructed is equal to the number of variable initializers immediately enclosed by the braces of the array initializer. Space is allocated for a new array of that length. If there is insufficient space to allocate the array, evaluation of the array initializer completes abruptly by throwing an The variable initializers immediately enclosed by the braces of the array initializer are then executed from left to right in the textual order they occur in the source code. The n'th variable initializer specifies the value of the n-1'th array component. If execution of a variable initializer completes abruptly, then execution of the array initializer completes abruptly for the same reason. If all the variable initializer expressions complete normally, the array initializer completes normally, with the value of the newly initialized array. If the component type is an array type, then the variable initializer specifying a component may itself be an array initializer; that is, array initializers may be nested. In this case, execution of the nested array initializer constructs and initializes an array object by recursive application of the algorithm above, and assigns it to the component. Example 10.6-1. Array Initializers class Test { public static void main(String[] args) { int ia[][] = { {1, 2}, null }; for (int[] ea : ia) { for (int e: ea) { System.out.println(e); } } } } This program produces the output: 1 2 before causing a 10.7. Array MembersThe members of an array type are all of the following:
An array thus has the
same class A<T> implements Cloneable, java.io.Serializable { public final int length = X ; public T[] clone() { try { return (T[])super.clone(); // unchecked warning } catch (CloneNotSupportedException e) { throw new InternalError(e.getMessage()); } } } Note that the cast in the example above would generate an unchecked warning (§5.1.9) if arrays were really implemented this way. See §9.6.3.4 for
another situation where the difference between Example 10.7-1. Arrays Are Cloneable class Test1 { public static void main(String[] args) { int ia1[] = { 1, 2 }; int ia2[] = ia1.clone(); System.out.print((ia1 == ia2) + " "); ia1[1]++; System.out.println(ia2[1]); } } This program produces the output: false 2 showing that the components of the arrays referenced by Example 10.7-2. Shared Subarrays After A Clone The fact that subarrays are shared when a multidimensional array is cloned is shown by this program: class Test2 { public static void main(String[] args) throws Throwable { int ia[][] = { {1,2}, null }; int ja[][] = ia.clone(); System.out.print((ia == ja) + " "); System.out.println(ia[0] == ja[0] && ia[1] == ja[1]); } } This program produces the output: false true showing that the 10.8. Class Objects for ArraysEvery array has an associated Example 10.8-1. class Test { public static void main(String[] args) { int[] ia = new int[3]; System.out.println(ia.getClass()); System.out.println(ia.getClass().getSuperclass()); } } This program produces the output: class [I class java.lang.Object where the string " Example 10.8-2. Array class Test { public static void main(String[] args) { int[] ia = new int[3]; int[] ib = new int[6]; System.out.println(ia.getClass() == ib.getClass()); System.out.println("ia has length=" + ia.length); } } This program produces the output: true ia has length=3 The program uses the method 10.9. An Array of Characters is Not a StringIn the Java programming language,
unlike C, an array of A The method What is a named unit of data that is assigned a value?A variable is a named unit of data that is assigned a value. If the value is modified, the name does not change. Variables are used with most programming languages and come in many forms, defined by the script or software programmer. Some variables are mutable, meaning their values can change.
Which of the following describes a data item with a name and a value that remain the same during the execution of a program?A constant is a data item whose value cannot change during the program's execution. Thus, as its name implies – the value is constant. A variable is a data item whose value can change during the program's execution.
What is a named memory location that you can use to store a value?A constant is a named memory location which temporarily stores data that remains the same throughout the execution of the program. The type of a variable indicates what kind of value it will store.
What is the name given to an identifier that always holds the same value?Another important use for identifiers is giving names to values that remain the same throughout a program. These values are called constants.
|