The basic concepts of Java constant pool

1. What is a constant
with the final member variable represents a modified constant value once given can not be changed!

There are three variables final modification: static variables, local variables and instance variables, the three types of constants, respectively.

2, Class file constant pool

In the Class file structure, most of the first four bytes for the magic number Magic Number, for determining whether a file can be accepted JVM, followed by 4 bytes used to store the version number, the first two bytes of storage minor version number, the version number of the main memory 2, and then followed by a constant pool for storing constants, since the number of constants is not fixed, so constant pool entry U2 placing a type of data (constant_pool_count) the storage capacity of the constant pool count.

The constant pool is mainly used for storage of two categories constants: literal (Literal) and symbolic reference amount (Symbolic References), literal constant level equivalent to the concept of the Java language, such as text strings, declared as a constant value final and so on,

Symbol reference compiler theory belongs to the concept of areas, including the following three types of constants:

a, classes and interfaces of the fully qualified name

b, the field names and descriptors

c, the method name and descriptor

  

3, the method of operating time constant pool area

Runtime constant pool is part of the zone method.

CLass file versions except classes, fields, methods, and interface description information, as well as a constant pool information, and for storing various literal compile generated reference symbols, this part will load the class often run into the method area of ​​the pool volume storage.

Another important feature of the runtime constant pool file relative to the CLass with constant pool is dynamic, Java language does not require constant necessarily only compile to produce, that is not preset the content CLass file constant pool to enter the zone method runtime constant pool, during operation may be new constant into the pool, take advantage of this feature is the developer is more intern () method of the String class.

4, constant pool of benefits

The constant pool is to avoid frequently create and destroy objects affect system performance, which achieves the shared object.

For example, a string constant pool, at compile time to put all the string literals into a constant pool.

(1) saving memory space: all the same string constants in the constant pool is combined, occupies only a space.

(2) save operating time: string comparisons == ratio equals () fast. For the two reference variables, only a == determines whether reference equality, it can be determined whether the actual values ​​are equal.

  

5, double equal sign == meaning

Application of double equal sign between basic data types, is their comparison values.

Application of double equal sign between the complex data type (type), is that they compare the address stored in memory.

Guess you like

Origin blog.51cto.com/14512197/2444528