首页 > 编程语言 > 详细

JVM 数组创建的本质

时间:2019-06-08 13:31:34      阅读:123      评论:0      收藏:0      [点我收藏+]

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)

JVM 数组创建的本质

原文:https://www.cnblogs.com/linlf03/p/10990145.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!