Creation and management of Java string string objects

Source: Internet
Author: User
Tags code tag constant what constant

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.