Understanding of JVM Running Time Pool in java Learning

  • 2021-11-14 05:45:24
  • OfStack

Running constant measuring pool

At runtime, the constant pool is part 1 of the method area. In addition to the description information of class version, field, method and interface, there is also an information constant measurement pool in Class file, which is used to store various literal quantities and symbol references generated at compile time. This part of content will be stored in the running constant measurement pool in the method area after the class is loaded.

Another important characteristic of running constant pool compared with Class file constant pool is that it is dynamic. Java language does not require constant 1 to be generated only at compile time, that is, the content of constant pool that is not preset in Class file can enter the method area running constant pool, running constant pool, and new constants may be put into the pool during running. This feature is widely used by developers is intern () method of String class.

Benefits of constant pool

Constant pool is to avoid frequent creation and destruction of objects and affect system performance, and it realizes the sharing of objects.
For example, string constant pool, all strings are put into one constant pool during compilation.
(1) Save memory space: All the same string constants in the constant pool are merged, occupying only 1 space.
(2) Save running time: When comparing strings, it is faster than equals (). For two reference variables, only "" is used to judge whether the references are equal, and the actual values can be judged to be equal.

Double equal signs are applied between basic data types, and their numerical values are compared. Double equal signs are applied between composite data types, and their storage addresses in memory are compared.

Wrapper classes and constant pools of basic types

Most of the basic types of wrapper classes in Java implement constant pool technology, namely: Byte, Short, Integer, Long, Character, Boolean.


public class NotInitialzation {
    public static void main(String[] args) {
//        System.out.println(SubClass.value);
//        SubClass sbc = new SubClass();
        Integer inte = 10;
        Integer inte1 = 3 + 4;
        Integer a = new Integer(3);
        Integer b = new Integer(4);
        Integer c = a + b;
        Integer d = new Integer(7);
        System.out.println(inte1 == c);//true
        System.out.println(c == a + b);//true
        System.out.println(c == d);//false
        System.out.println("=====================================================");
        String str = "abcd";
        String str1 = "ab";
        String str2 = "abcd" + "ab";
        String str3 = str + str1;
        String str4 = "abcdab";
        System.out.println(str3 == str2);//false
        System.out.println(str2 == str4);//true

    }
}

Decompile the compiled class file:


// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   NotInitialzation.java
import java.io.PrintStream;
public class NotInitialzation
{
	public NotInitialzation()
	{
	}
	public static void main(String args[])
	{
		Integer inte = Integer.valueOf(10);
		Integer inte1 = Integer.valueOf(30);
		Integer a = new Integer(3);
		Integer b = new Integer(4);
		Integer c = Integer.valueOf(a.intValue() + b.intValue());
		Integer d = new Integer(7);
		System.out.println(c.intValue() == a.intValue() + b.intValue());
		System.out.println(c == d);
		System.out.println("=====================================================");
		String str = "abcd";
		String str1 = "ab";
		String str2 = "abcdab";
		String str3 = (new StringBuilder()).append(str).append(str1).toString();
		String str4 = "abcdab";
		System.out.println(str3 == str2);
		System.out.println(str2 == str4);
	}
}

Integer inte = 10; java will directly encapsulate the code as Integer inte = Integer. valueOf (10) when compiling; To use the objects in the constant pool.

Literal and symbolic references

Baidu Encyclopedia explains: In computer science, literal quantity (literal) is a representation used to express a fixed value in source code (notation). Almost all computer programming languages have literal representation of basic values, such as integers, floating-point numbers and strings; And many also support literal representation for Boolean values; Others even support literal notation for elements of enumerated types and values of composite types such as arrays, records, and objects;

Above is java learning JVM running time pool understanding details, more information about JVM running time pool please pay attention to other related articles on this site!


Related articles: