1、创建数组
创建一个MyParent4[] 数组
public class MyTest4 { public static void main(String[] args) { MyParent4[] myParent4s = new MyParent4[1]; System.out.println(myParent4s.getClass()); } } class MyParent4{ static { System.out.println("MyParent4 static block"); } }
打印结构
说明此处并不符合主动使用的场景,所有不会初始化MyParent4。 打印myParent4s.getClass()的结果为数组类型。[Lcom.example.jvm.classloader.MyParent4 这个类型是JVM在运行期创建出来的。
2、完整的创建数组代码
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()); } } class MyParent4{ static { System.out.println("MyParent4 static block"); } }
打印结果
class [Lcom.example.jvm.classloader.MyParent4; class [[Lcom.example.jvm.classloader.MyParent4; class java.lang.Object class java.lang.Object class [I class java.lang.Object
反编译
Compiled from "MyTest4.java" public class com.example.jvm.classloader.MyTest4 { public com.example.jvm.classloader.MyTest4(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: iconst_1 1: anewarray #2 // class com/example/jvm/classloader/MyParent4 4: astore_1 5: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 8: aload_1 9: invokevirtual #4 // Method java/lang/Object.getClass:()Ljava/lang/Class; 12: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V 15: iconst_1 16: iconst_1 17: multianewarray #6, 2 // class "[[Lcom/example/jvm/classloader/MyParent4;" 21: astore_2 22: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 25: aload_2 26: invokevirtual #4 // Method java/lang/Object.getClass:()Ljava/lang/Class; 29: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V 32: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 35: aload_1 36: invokevirtual #4 // Method java/lang/Object.getClass:()Ljava/lang/Class; 39: invokevirtual #7 // Method java/lang/Class.getSuperclass:()Ljava/lang/Class; 42: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V 45: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 48: aload_2 49: invokevirtual #4 // Method java/lang/Object.getClass:()Ljava/lang/Class; 52: invokevirtual #7 // Method java/lang/Class.getSuperclass:()Ljava/lang/Class; 55: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V 58: iconst_1 59: newarray int 61: astore_3 62: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 65: aload_3 66: invokevirtual #4 // Method java/lang/Object.getClass:()Ljava/lang/Class; 69: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V 72: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 75: aload_3 76: invokevirtual #4 // Method java/lang/Object.getClass:()Ljava/lang/Class; 79: invokevirtual #7 // Method java/lang/Class.getSuperclass:()Ljava/lang/Class; 82: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V 85: return }
助记符:
anewarray: 表示创建一个引用类型的(如类、接口、数组)数组,并将其引用值压如栈顶 (1: anewarray #2)
newarray: 表示创建一个指定的原始类型(如int,float,char等)的数组,并将其引用值压入栈顶 ( 59: newarray int)
原文:https://www.cnblogs.com/linlf03/p/10990145.html