JVM学习第四天-编译器运行期常量的区别及数组创建

JVM学习第四天-编译器运行期常量的区别及数组创建

首先我们用一串代码来看一下运行期间的常量会不会初始化我们的类

public class MyTest3 {
    public static void main(String[] args) {
        System.err.println(MyParent3.str);
    }
}

class MyParent3{
    public static final String str = UUID.randomUUID().toString();

    static{
        System.err.println("MyParent3 static code");
    }
}

运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VmTHGswa-1580808382201)(E:\Typora\图片\Mtest3.1.png)]

然后我们刚刚上一篇的列子:

public class MyTest2 {
    public static void main(String[] args) {
        System.err.println(MyPreant2.str);
    }
}
class MyPreant2{
    public static final String str ="hello world";
    static {
        System.err.println("MyParent2 static block");
    }

}

运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KSEsIGJ3-1580808382202)(E:\Typora\图片\Mtest2.1.png)]

​ 那么区别在哪里大家都显而易见了MyPreant2没有进行初始化但是MyParent3进行了初始化,那么照成这个的原因就是UUID.randomUUID().toString(),这一串代码表示编译期间不是固定值,当运行加载UUID.randomUUID()这个方法的时候才会固定值,所以才会导致MyParent3初始化。

​ 那么我学习的时候看视频直接删掉MyParent3.class文件导致运行错误所以才证明这个类已经被初始化了。

小总结:

/**
 * 当一个常量的值并非编译期间可以确定的,那么其值就不会被放到调用类的常量池中
 * 这时在程序运行时,会导致主动使用这个常量所在的类,显然会导致这个类被初始化
 */

那么就是下一个案列:

public class MyTest4 {
    public static void main(String[] args) {
       // MyParent4 myParent4=new MyParent4();
        MyTest4[] myTest41=new MyTest4[1];
        //查看类型
        System.err.println(myTest41.getClass());
        MyTest4[][] myTest42=new MyTest4[1][1];
        System.err.println(myTest42.getClass());
        //查看父类类型
        System.err.println(myTest41.getClass().getSuperclass());
        System.err.println(myTest42.getClass().getSuperclass());
        System.err.println("===============");
        int[] ints=new int[1];
        System.err.println(ints.getClass());
        System.err.println(ints.getClass().getSuperclass());
        char[]  chars=new char[1];
        System.err.println(chars.getClass());
        System.err.println(chars.getClass().getSuperclass());
        boolean[] booleans=new boolean[1];
        System.err.println(booleans.getClass());
        System.err.println(booleans.getClass().getSuperclass());
        short[] shorts=new short[1];
        System.err.println(shorts.getClass());
        System.err.println(shorts.getClass().getSuperclass());
        byte[] bytes=new byte[1];
        System.err.println(bytes.getClass());
        System.err.println(bytes.getClass().getSuperclass());

    }
}
class MyParent4{
    static {
        System.err.println("MyParent4 static block");
    }

}

运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9omjQoBZ-1580808382202)(E:\Typora\图片\Mtest4.1.png)]

MyTest4总结:

/**
 * 对于数组实列来说,其类型是由于JVM在运行期间动态生成的,表示为[Lcom....
 * 这种形式。动态生成类型,其父类行就是Object。
 *
 * 对于数组来说,JavaDoc经常将构成数组的元素Componentt,实际上就是将数组降低一个维度后的类型。
 */

助记符

  • anewarray:表示创建一个引用类型的(如类、接口,数组)数组,并将其引用值压入栈顶
    JavaDoc经常将构成数组的元素Componentt,实际上就是将数组降低一个维度后的类型。
    */

## 助记符

+ anewarray:表示创建一个引用类型的(如类、接口,数组)数组,并将其引用值压入栈顶
+ newarray:表示创建一个指定的原始类型(如int,float,char等)数组,并将其引用值压入栈顶
发布了5 篇原创文章 · 获赞 1 · 访问量 194

猜你喜欢

转载自blog.csdn.net/weixin_44281696/article/details/104172295
今日推荐