首页 > 其他 > 详细

验证常量池中的字符串仅是符号,第一次用到时才变为对象

时间:2019-12-08 14:54:29      阅读:95      评论:0      收藏:0      [点我收藏+]

接上文连接:StringTable

测试用例:

/**
 * 演示字符串字面量也是【延迟】成为对象的
 */
public class TestString {
    public static void main(String[] args) {
        int x = args.length;
        System.out.println(); // 字符串个数 2275

        System.out.print("1");
        System.out.print("2");
        System.out.print("3");
        System.out.print("4");
        System.out.print("5");
        System.out.print("6");
        System.out.print("7");
        System.out.print("8");
        System.out.print("9");
        System.out.print("0");
        System.out.print("1"); // 字符串个数 2285
        System.out.print("2");
        System.out.print("3");
        System.out.print("4");
        System.out.print("5");
        System.out.print("6");
        System.out.print("7");
        System.out.print("8");
        System.out.print("9");
        System.out.print("0");
        System.out.print(x); // 字符串个数
    }
}

如何测试呢,我们利用IDEA中的debug模式下的Memory框进行检测

技术分享图片

这个Memory是用来分析jvm堆中的对象。

引用一下IDEA的官方介绍吧。

Memory官方文档

The Memory view shows you the total number of objects in the heap:

When you step over the code, the Diff column shows how the number of objects changes between the debugger stops, which helps you see how the code you are stepping affects the heap.

Double-click a class name to view all instances of this class:


以Debug模式运行程序

技术分享图片


技术分享图片


技术分享图片

可以发现还是2365,观察程序,因为运行时常量池中已经有了该对象了, 自然不会再重复创建了。

验证常量池中的字符串仅是符号,第一次用到时才变为对象

原文:https://www.cnblogs.com/heliusKing/p/12005501.html

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