对于数组实例来说,其类型是由JVM在运行期动态生成的,表示为:[L元素类名
动态生成的类型,其父类型就是Object
并且new数组时,数组元素的类并不会被初始化
助记符:
anewarray:表示创建一个引用类型的(如类,接口,数组)数组,并将其引用值压入栈顶
newarray:表示创建一个指定的原始类型(如int,float,char)数组,并将其引用值压入栈顶
下面是代码演示:
public class MyTest4 {
public static void main(String[] args) {
MyParent4[] myParent4s=new MyParent4[1];
System.out.println(myParent4s.getClass());
MyParent4[][] myParent4s1=new MyParent4[1][1];
System.out.println(myParent4s1.getClass());
System.out.println(myParent4s.getClass().getSuperclass());
System.out.println(myParent4s1.getClass().getSuperclass());
int[] ints =new int[1];
System.out.println(ints.getClass());
System.out.println(ints.getClass().getSuperclass());
char[] chars =new char[1];
System.out.println(chars.getClass());
boolean[] booleans=new boolean[1];
System.out.println(booleans.getClass());
short[] shorts=new short[1];
System.out.println(shorts.getClass());
byte[] bytes=new byte[1];
System.out.println(bytes.getClass());
}
class MyParent4{
static {
System.out.println("myparent4 static block");
}
输出结果:
接口当中的成员变量都是常量,子接口继承父接口时,调用子接口的常量,两个接口都不会被初始化
只有在真正使用到父接口时,如引用父接口当中所定义的常量时,才会初始化
/*
* 当一个接口在加载时,并不要求其父接口都完成了加载
* */
public class MyTest5 {
public static void main(String[] args) {
System.out.println(MyChild5.b);
}
}
interface MyParent5{
public static final int a=5;//接口中public static final都是冗余的
}
interface MyChild5 extends MyParent5{
public static int b=6;
}
如果把Myparent5.class和Mychild5,class删掉,结果仍然是:
关键点在于有没有final,有final 的话就不会触发类加载,没有的话就会触发其父类或者所实现接口的加载(接口中的成员变量都是带final 的常量)
当一个类或接口在初始化时,并不要求其实现的接口或父接口都完成了初始化
/*
* 当一个类或接口在初始化时,并不要求其实现的接口或父接口都完成了初始化
* */
public class MyTest5 {
public static void main(String[] args) {
System.out.println(MyChild5.b);
}
}
interface MyParent5{
Thread thread=new Thread(){
{
System.out.println("Myparent5 invoked");
}
};
}
class MyChild5 implements MyParent5{
public static int b=6;
}
输出结果:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。