Constant pool = constant pool = constant pool = constant pool = constant pool = constant pool
Constant pool
package com.example.demo;
/ * * *@author zinsanity
* @dateThe 2020-11-21 16:06 *@desc* /
public class Review {
public static void main(String[] args) {
String info = "hello world";
int a = Awesome!;
final int b = 66;
int c = 36728; }}Copy the code
After this code is compiled, use the javap -v review.class command to view the bytecode
Classfile /D:/idea_projects/demo/target/classes/com/example/demo/Review.class
Last modified 2020-11-21; size 561 bytes
MD5 checksum 09914fecad4a776e7ac86d1a6fd43baa
Compiled from "Review.java"
public class com.example.demo.Review
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #5.#26 // java/lang/Object."<init>":()V
#2 = String #27 // hello world
#3 = Integer 36728
#4 = Class #28 // com/example/demo/Review
#5 = Class #29 // java/lang/Object
#6 = Utf8 <init>
#7 = Utf8 ()V
#8 = Utf8 Code
#9 = Utf8 LineNumberTable
#10 = Utf8 LocalVariableTable
#11 = Utf8 this
#12 = Utf8 Lcom/example/demo/Review;
#13 = Utf8 main
#14 = Utf8 ([Ljava/lang/String;)V
#15 = Utf8 args
#16 = Utf8 [Ljava/lang/String;
#17 = Utf8 info
#18 = Utf8 Ljava/lang/String;
#19 = Utf8 a
#20 = Utf8 I
#21 = Utf8 b
#22 = Utf8 c
#23 = Utf8 MethodParameters
#24 = Utf8 SourceFile
#25 = Utf8 Review.java
#26 = NameAndType #6:#7 // "<init>":()V
#27 = Utf8 hello world
#28 = Utf8 com/example/demo/Review
#29 = Utf8 java/lang/Object
{
public com.example.demo.Review();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 8: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/example/demo/Review;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=5, args_size=1
0: ldc #2 // String hello world
2: astore_1
3: sipush 666
6: istore_2
7: bipush 66
9: istore_3
10: ldc #3 // int 36728
12: istore 4
14: return
LineNumberTable:
line 10: 0
line 11: 3
line 12: 7
line 13: 10
line 14: 14
LocalVariableTable:
Start Length Slot Name Signature
0 15 0 args [Ljava/lang/String;
3 12 1 info Ljava/lang/String;
7 8 2 a I
10 5 3 b I
14 1 4 c I
MethodParameters:
Name Flags
args
}
SourceFile: "Review.java"
Copy the code
You can see the Constant Pool in the bytecode file. That is, every class file has a constant pool. Let’s start with this bytecode, which is the bytecode compiled by our main method, where 0: LDC #2 means to fetch the second location from the constant pool and store it in astore_1. So let’s go to the constant pool
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=5, args_size=1
0: ldc #2 // String hello world
2: astore_1
3: sipush 666
6: istore_2
7: bipush 66
9: istore_3
10: ldc #3 // int 36728
12: istore 4
14: return
Copy the code
In a bytecode file, the following snippet represents the constant pool. #2 corresponds to String, and then you have to find #27, which corresponds to Hello Word. So, 0: LDC #2 finally takes a Hello Word String of type String from the constant pool and stores it into ASTore_1. Sipush stores 666 to istore_2, bipush stores 66 to istore_3, and LDC goes to the constant pool to store data at #3.
Constant pool: #1 = Methodref #5.#26 // java/lang/Object."<init>":()V ...... # 29Copy the code
First question: why are all types of int data using different instructions? According to the SPECIFICATION of the JVM, different instructions are used to push int values according to the int value range. Second question: why are 666 and 66 not in the constant pool? Int = 36728; int = 36728; int = 36728;Copy the code
Run-time constant pool
In the constant pool, you can see that these are all temporary symbols like #1, #2, and #3. When a program is run, the JVM adds all the bytecode files to memory and, after linking and validating, converts the #1, #2, and #3 symbols into the actual address in memory and runs them in the runtime constant pool. The run-time constant pool is held in the method area, which is globally unique, and is an area shared by all classes.
String constant pool
For instance
String a = "test1";
String b = "test2";
Copy the code
The strings “test1” and “test2” are first stored in the constant pool after compilation. When the program runs, the string is loaded into the string constant pool after loading into memory. After jdk1.7, the string constant pool is in the heap.