The concept of Constant pool constant pools:
When it comes to some special cases of string, it always mentions string pool or constant pool, but I think a lot of people don't quite understand what constant pool is, where it's stored, so let's talk about constant The contents of the pool.
String pool is the area that corresponds to storing a string constant in the Constant pool. The custom is called string pool, and it is also known as String Constant pool. There seems to be no formal naming.
In the Java compiled class file, there is a region called constant Pool, which is a table of arrays, type Cp_info constant_pool[], used to store constants used in the program, including class/string/ A variety of basic Java data types, such as Integer.
For constant Pool, the basic general structure of the table is:
cp_info {
u1 tag;
u1 info[];
}
Tag is a number that represents the type of stored constants, such as 8 for the string type, and 5 for the long type, info[] based on
The difference between the type code tag will change accordingly.
For string types, the structure of the table is:
CONSTANT_String_info {
u1 tag;
u2 string_index;
}
The tag is fixed to 8,string_index is the string content information, the type is:
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}
The tag is fixed to 1,length as the length of the string, Bytes[length] as the content of the string.
(The following code is compiled in JDK6)
To understand the structure of the constant pool in detail, we'll refer to some code:
String s1 = "sss111";
String s2 = "sss222";
System.out.println(s1 + " " + s2);
Because both "sss111" and "sss222" are string constants, they are created at compile time and stored in the class file.
The corresponding representations of these 2 constants exist in the compiled class file:
08 00 11 01 00 06 73 73 73 31 31 31 08 00 13 01 ; ......sss111....
00 06 73 73 73 32 32 32 ; ..sss222