Verification string constant pool is only a symbol, when used for the first time become the object

Connect Wen connection: StringTable

Test Case:

/**
 * 演示字符串字面量也是【延迟】成为对象的
 */
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); // 字符串个数
    }
}

How to test it, we use the Memory box under IDEA in debug mode detection

This Memoryis used to analyze the object jvm heap.

To quote the official introduction IDEA it.

Memory official documents

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:


Run the program in Debug mode



2365 still can be found, the observation program, as has been the object of the runtime constant pool, naturally would not repeat created.

Guess you like

Origin www.cnblogs.com/heliusKing/p/12005501.html