The Java language has two kinds of types: simple and composite. Simple types are those that cannot be broken down; they are atomic. The integer, floating point, boolean, and character types are all simple types. Composite types are built on simple types. The language has three kinds of composite types: arrays, classes, and interfaces. Simple types and arrays are discussed in this section.
A variable's type does not directly affect its storage allocation. Type only determines a variable's arithmetic properties and legal range of values. If a value is assigned to a variable that is outside the legal range of the variable, the value is reduced modulo the range.
Floating point arithmetic and data formats are defined by IEEE 754. See Appendix: Floating Point for details on the floating point implementation.
Boolean values are not numbers and cannot be converted into numbers by casting.
char s[] = new char[30];The first element of an array is at index 0 (zero). Specifying dimensions in the declarations is not allowed. Every allocation of an array must be explicit--use new every time:
int i[] = new int[3];The language does not support multi-dimensional arrays. Instead, programmers can create arrays of arrays:
int i[][] = new int[3][4];At least one dimension must be specified but other dimensions can be explicitly allocated by a program at a later time. For example:
int i[][] = new int[3][];is a legal declaration.
In addition to the C-style array declaration, where brackets follow the name of the variable or method, Java allows brackets following the array element type. The following two lines are equivalent:
int iarray[]; int[] iarray;as are the following method declarations:
byte f( int n )[]; byte[] f( int n );Subscripts are checked to make sure they're valid:
int a[] = new int[10]; a[5] = 1; a[1] = a[0] + a[2]; a[-1] = 4; // Throws an ArrayIndexOutOfBoundsException // at runtime a[10] = 2; // Throws an ArrayIndexOutOfBoundsException // at runtimeArray dimensions must be integer expressions:
int n; ... float arr[] = new float[n + 1];The length of any array can be found by using .length:
int a[][] = new int[10][3]; println(a.length); // prints 10 println(a[0].length); // prints 3
new Thread[n]creates an instance of Thread[]. If class A is a superclass of class B (i.e., B extends A) then A[] is a superclass of B[] (see the diagram below).
Hence, you can assign an array to an Object:
Object o; int a[] = new int[10]; o = a;and you can cast an Object to an array:
a = (int[])o;Array classes cannot be explicitly subclassed.
The Java Language Specification
Generated with CERN WebMaker