在Java语言规范中,有关数组索引类型有下列描述:
Arrays must be indexed by int values; short , byte , or char values may also be used as index values because they are subjected to unary numeric promotion and become int values.
An attempt to access an array component with a long index value results in a compile-time error.
大意为:
数组的索引类型必须是int类型;但byte,short或char类型的值也能当数组索引,因为它们会自动转换成int类型。
当数组索引的类型是long类型时,程序会在编译时报错。
示例:
public class ListTest{
public static void main(String[] args) {
long[] arr = new long[3L];
System.out.println(arr.length);
}
}
结果: