1 数组是引用类型
2 java虚拟机在装入数组时,会根据数组元素的类型以及数组维度创建对应的Class对象,数组元素的类型和维度唯一确认了数组类的Class对象,比如下面的代码:
public static void main (String[] args) {
int[] a = new int[5];
System.out.println(a);
String[] b = new String[5];
System.out.println(b);
ForkJoinDemo[] c = new ForkJoinDemo[5];
System.out.println(c);
String[][] aa = new String[3][];
aa[0] = new String[3];
System.out.println(aa[0]);
ForkJoinDemo[][] cc = new ForkJoinDemo[3][];
cc[0] = new ForkJoinDemo[3];
System.out.println(cc);
System.out.println(cc[0]);
}
打印结果:
[I@136a43c
[Ljava.lang.String;@589e56
[Lcom.malone.threadCurr.ForkJoinDemo;@3411a
[Ljava.lang.String;@edf3f6
[[Lcom.malone.threadCurr.ForkJoinDemo;@2bc3f5
[Lcom.malone.threadCurr.ForkJoinDemo;@14e3f41
打印结果前半段为class的名称;由此可以知道虚拟机会为根据数组元素的类型和数组的维度创建Class对象,类命名规则为:
1 根据维度决定[的个数,维度为1则以一个[开头,为2以两个[[开头,以此类推
2 如果数组元素的类型为基础数据类型,比如int, long 则以首字母大写跟在[后面
3 如果数组元素类型为引用类型,则在[后面加一个大写的L然后再引用类的名称
虚拟机根据数组元素类型和维度来唯一创建类:
public class ForkJoinDemo {
public static void main (String[] args) {
int[] a = new int[5];
System.out.println(a.getClass());
int[] b = new int[100];
System.out.println(b.getClass());
System.out.println(a.getClass() == b.getClass());
ForkJoinDemo[] c = new ForkJoinDemo[5];
System.out.println(c.getClass());
ForkJoinDemo[] d = new ForkJoinDemo[100];
System.out.println(d.getClass());
System.out.println(c.getClass() == d.getClass());
}
}
打印结果:
class [I
class [I
true
class [Lcom.malone.threadCurr.ForkJoinDemo;
class [Lcom.malone.threadCurr.ForkJoinDemo;
true