zoukankan      html  css  js  c++  java
  • 验证常量池中的字符串仅是符号,第一次用到时才变为对象

    接上文连接: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,观察程序,因为运行时常量池中已经有了该对象了, 自然不会再重复创建了。

  • 相关阅读:
    pthread条件变量
    c++信号处理
    必杀技
    待飞日记(第四天和第五天)
    c++面试题总结(2)
    比起主流的30秒,10秒广告能获得2倍的效果
    c++面试题总结(1)
    待飞日记(第三天)
    static_cast, dynamic_cast, const_cast探讨
    c++一些问题总结
  • 原文地址:https://www.cnblogs.com/heliusKing/p/12005501.html
Copyright © 2011-2022 走看看