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)
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
The meaning of the double equal sign ==4. Basic types of packaging classes and constant poolsMost 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
- 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.
Float and Double do not implement constant pool technology.
1) Integer and constant pooli4 == 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.
"abcd"是在常量池中拿对象,new String("abcd")是直接在堆内存空间创建一个新的对象。只要使用new方法,便需要创建新的对象。
连接表达式 +,只有使用引号包含文本的方式创建的String对象之间使用“+”连接产生的新对象才会被加入常量池中。
对于字符串变量的“+”连接表达式,它所产生的新对象都不会被加入字符串池中,其属于在运行时创建的字符串,具有独立的内存地址,所以不引用自同一String对象。
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,它们不是同一个对象"); } }
s不等于t,它们不是同一个对象。A和B虽然被定义为常量,但是它们都没有马上被赋值。在运算出s的值之前,他们何时被赋值,以及被赋予什么样的值,都是个变数。因此A和B在被赋值之前,性质类似于一个变量。那么s就不能在编译期被确定,而只能在运行时被创建了。
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
String的intern()方法会查找在常量池中是否存在一份equal相等的字符串,如果有则返回该字符串的引用,如果没有则添加自己的字符串进入常量池。
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!