JVM 數組創建的本質


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)


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com