Java Reference
In-Depth Information
10.8.
Class
Objects for Arrays
Every array has an associated
Class
object, shared with all other arrays with the same com-
ponent type.
Example 10.8-1.
Class
Object Of Array
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 “
[I
” is the run-time type signature for the class object “array with
component type
int
”.
Example 10.8-2. Array
Class
Objects Are Shared
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
getClass
inherited from class
Object
, and the field
length
.
The result of the comparison of the
Class
objects in the first
println
demonstrates that all