Home  >  Article  >  Java  >  java virtual machine: runtime constant pool

java virtual machine: runtime constant pool

巴扎黑
巴扎黑Original
2017-06-26 09:56:061474browse

1. Introduction to the Runtime Constant Pool

The Runtime Constant Pool (Runtime Constant Pool) is part of the method area. ClassIn addition to the class version, fields, methods, interfaces and other description information, there is also a information that is the constant pool (Constant Pool Table), which is used to store the compile-time The generated various literals and symbol references, this part will be stored in the constant pool after the class is loaded.

Runtime constants are relative to constants, and they have an important feature: dynamics. Of course, dynamic constants with the same value are only from different sources than the constants we usually talk about, but they are all stored in the same memory area in the pool. The Java language does not require that constants must only be generated during compilation. New constants may also be generated during runtime. These constants are placed in the runtime constant pool. The constants mentioned here include: basic typePackaging class (Packaging class does not manage floating point types, integer only manages -128 to 127) and String (can also be passed String.intern() Method can force String to be put into the constant pool)

2. Information constant pool in the Class file

In the Class file structure, the first 4 bytes are used to store Megic Number, used to determine whether a file can be accepted by the JVM, and then 4 bytes are used to store the version number. The first 2 bytes store the minor version number, the last 2 bytes store the major version number, and then are used to store constants. Since the number of constants is not fixed, a U2 type data (constant_pool_count) is placed at the entrance of the constant pool to store the constant pool capacity count value.

The constant pool is mainly used to store two types of constants: literals and symbolic references. Literals are equivalent to the concept of constants at the Java language level, such as text strings, declared as final. Constant values, etc. Symbol reference is a concept related to compilation principles, including the following three types of constants:

  • Fully qualified names of classes and interfaces

  • Field names and descriptors

  • Method names and descriptors

##3. Benefits of constant pool

The constant pool is to avoid frequent creation and destruction of objects that affects system performance, and it implements object sharing. For example, the string constant pool puts all string literals into a constant pool during the compilation phase.

  • Save memory space: All identical string constants in the constant pool are merged and only occupy one space.

  • Save running time: When comparing strings, == is faster than equals(). For two reference variables, just use == to determine whether the references are equal, and you can also determine whether the actual values ​​are equal.

The meaning of the double equal sign ==

  • The double equal sign is applied between basic data types, and their numerical values ​​are compared. .

  • Double equal signs are applied between composite data types (classes), and their storage addresses in memory are compared.

4. Basic types of packaging classes and constant pools

Most of the basic types of packaging classes in Java implement constant pool technology, namely Byte ,Short,Integer,Long,Character,Boolean. These five packaging classes create corresponding types of cache data with values ​​​​[-128, 127] by default, but new objects will still be created beyond this range. The wrapper classes of the two floating point types

Float and Double do not implement constant pool technology.

1) Integer and constant pool

java virtual machine: runtime constant pool
Integer i1 = 40;
Integer i2 = 40;
Integer i3 = 0;
Integer i4 = new Integer(40);
Integer i5 = new Integer(40);
Integer i6 = new Integer(0);

System.out.println("i1=i2   " + (i1 == i2));
System.out.println("i1=i2+i3   " + (i1 == i2 + i3));
System.out.println("i1=i4   " + (i1 == i4));
System.out.println("i4=i5   " + (i4 == i5));
System.out.println("i4=i5+i6   " + (i4 == i5 + i6));  
System.out.println("40=i5+i6   " + (40 == i5 + i6));


i1=i2   true
i1=i2+i3   true
i1=i4   false
i4=i5   false
i4=i5+i6   true
40=i5+i6   true
java virtual machine: runtime constant pool
Explanation:
  • Integer i1=40; Java will directly encapsulate the code into Integer i1=Integer.valueOf(40); when compiling, thereby using the constant pool objects in .

  • Integer i1 = new Integer(40); In this case, a new object will be created.

  • Statement

    i4 == i5 + i6, because the + operator does not apply to Integer objects, first i5 and i6 perform automatic unboxing operations and add values, i4 == 40. Then the Integer object cannot be directly compared with the numerical value, so i4 automatically unboxes it and converts it to the int value 40. Finally, this statement is converted to 40 == 40 for numerical comparison.

2) String and constant pool - ordinary method assignment

java virtual machine: runtime constant pool
String str1 = "abcd";
String str2 = new String("abcd");
System.out.println(str1==str2);//false

String str1 = "str";
String str2 = "ing";
String str3 = "str" + "ing";
String str4 = str1 + str2;
System.out.println("string" == "str" + "ing");// true
System.out.println(str3 == str4);//false

String str5 = "string";
System.out.println(str3 == str5);//true
java virtual machine: runtime constant pool
解释:
  • "abcd"是在常量池中拿对象,new String("abcd")是直接在堆内存空间创建一个新的对象。只要使用new方法,便需要创建新的对象

  • 连接表达式 +,只有使用引号包含文本的方式创建的String对象之间使用“+”连接产生的新对象才会被加入常量池中

  • 对于字符串变量的“+”连接表达式,它所产生的新对象都不会被加入字符串池中,其属于在运行时创建的字符串,具有独立的内存地址,所以不引用自同一String对象。

3)String与常量池-静态方法赋值

java virtual machine: runtime constant pool
public static final String A; // 常量A
public static final String B;    // 常量B
static {  
   A = "ab";  
   B = "cd";  
}  
public static void main(String[] args) {  
// 将两个常量用+连接对s进行初始化  
String s = A + B;  
String t = "abcd";  
if (s == t) {  
    System.out.println("s等于t,它们是同一个对象");  
  } else {  
    System.out.println("s不等于t,它们不是同一个对象");  
  }  
}
java virtual machine: runtime constant pool
解释:

s不等于t,它们不是同一个对象。A和B虽然被定义为常量,但是它们都没有马上被赋值。在运算出s的值之前,他们何时被赋值,以及被赋予什么样的值,都是个变数。因此A和B在被赋值之前,性质类似于一个变量。那么s就不能在编译期被确定,而只能在运行时被创建了。

4)String与常量池-intern方法

java virtual machine: runtime constant pool
public static void main(String[] args) {
  String s1 = new String("计算机");
  String s2 = s1.intern();
  String s3 = "计算机";
  System.out.println("s1 == s2? " + (s1 == s2));
  System.out.println("s3 == s2? " + (s3 == s2));
}
s1 == s2? false
s3 == s2? true
java virtual machine: runtime constant pool
解释:

String的intern()方法会查找在常量池中是否存在一份equal相等的字符串,如果有则返回该字符串的引用,如果没有则添加自己的字符串进入常量池

5)String与常量池-延伸

String s1 = new String("xyz"); //创建了几个对象?
解释:

考虑类加载阶段和实际执行时。

  • 类加载对一个类只会进行一次。”xyz”在类加载时就已经创建并驻留了(如果该类被加载之前已经有”xyz”字符串被驻留过则不需要重复创建用于驻留的”xyz”实例)。驻留的字符串是放在全局共享的字符串常量池中的。

  • 在这段代码后续被运行的时候,”xyz”字面量对应的String实例已经固定了,不会再被重复创建。所以这段代码将常量池中的对象复制一份放到heap中,并且把heap中的这个对象的引用交给s1 持有

这条语句创建了2个对象。

The above is the detailed content of java virtual machine: runtime constant pool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn