Introduction to Java constant pool parsing and string intern

Source: Internet
Author: User

Introduction to Java constant pool parsing and string intern

When a Java application is running, the Java Virtual Machine will save an internal runtime constant pool, which is different from the constant pool of the class file and is the data structure mapped to the Virtual Machine by the constant pool of the class file. For more information about the constant pool of class files, refer to the previous blog examples to explore Class files.

1. CONSTANT_Class entry Parsing

Symbol parsing for Array classes is special. If it is an array of basic types, the virtual machine will create a new array Class of this basic type and create a Class instance to represent this type. The definition Class loader of the array Class is the start Class loader. If an array of the reference type is used, resolution of the reference type will be performed before that. The definition class loader of the array class is the definition class loader of the reference type.

To resolve non-array classes and interfaces, follow these steps:

(1). Load this type and all its super types

If this type has been mounted to the current namespace of the VM before, you can directly use the loaded type. Otherwise, the initial class loader of the referenced initiating class is loaded. Superclasses of the target type must be loaded based on the current type, because only the current type is loaded, you can find its direct superclass symbolic reference from the super_class field of the class file, and then perform recursive parsing and loading until java. lang. object Class. In the recursive return process, the interfaces domain is checked to see which interfaces are implemented or extended, and the symbol reference to the interface is recursively traversed again.

(2). Check Access Permissions

The connection and initialization of the target type can be used normally. As mentioned above, initialization of the target type requires that all of its superclasses be initialized (superinterfaces are not required), and The superclasses have been loaded, therefore, you do not need to rely on the parsing order from the class to the Object class, but instead initialize the class from the Object class. Perform the following steps to connect and initialize a connection:

(3). type verification

(4). Type preparation

(5). Type resolution (postponed)

Note that this process is used to parse the referenced type and its superclass, because it is not immediately used for some symbolic references of the referenced type, therefore, this step is a process of parsing the referenced type of symbols in a strict sense. Only when you actively use the types pointed to by these symbolic references of the referenced types will these symbolic references be parsed to load, connect, and initialize the types they point.

(6). Type Initialization

2. CONSTANT_Fieldref entry Parsing

Because a type does not contain fields defined by its super type, the search for the target field starts from the type to which the field points, recursively search the implemented or extended interfaces, and Recursively search for the superclasses until the target field is found, and mark the field entry of the runtime constant pool as parsed, and change the data in the constant pool to direct reference to this field.

3. CONSTANT_Methodref entry Parsing

Similar to field search, but different from each other, the search sequence starts from this type, and then recursively searches for its superclass to Recursively search for its implemented or extended interface.

4. CONSTANT_InterfaceMethodRef entry Parsing

An interface method is searched recursively from the parsed interface to its superinterface.

5. CONSTANT_String entry Parsing

The Java Virtual Machine processes the string as a string object for maintenance, and the virtual machine maintains a string pool that contains references to all string objects that are "detained. To parse the CONSTANT_String constant pool, you must first check whether the reference of this string object exists in the string pool. If so, the constant pool data is directly resolved to the reference of this string object. If not, then you need to create a String object based on the string sequence, add its reference to the string pool, and parse the constant pool data into the reference.

You can also use the intern object of the String object to hold a String (note that it is not a String object). If there is a reference to the object of the String sequence in the String pool, you can directly return the reference. Otherwise, the String will be detained, but note that the reference to the String object returned will not point to the original String object because the original String object is located in the Java heap, the objects in the string pool are created by virtual machines and maintained by virtual machines.

 
 
  1. package com.ice.intern; 
  2.  
  3. public class InternTest { 
  4.  
  5.     public static void main(String args[]){ 
  6.         String a = new String("123"); 
  7.         String b = a; 
  8.         String c = new String("123");; 
  9.  
  10.         System.out.println("before intern:"); 
  11.         System.out.println("a = b ? :" + (a == b)); 
  12.         System.out.println("a = c ? :" + (a == c)); 
  13.  
  14.         a = a.intern(); 
  15.         c = c.intern(); 
  16.  
  17.         System.out.println("after intern:"); 
  18.         System.out.println("a = b ? :" + (a == b)); 
  19.         System.out.println("a = c ? :" + (a == c)); 
  20.     } 
  21.  

The result is as follows:

(6) analysis of other types (basic data types)

Directly use the constant value contained in the constant pool.

6. Direct reference

Constant pool parsing replaces symbol references with direct references. Direct reference to types, class variables, and class methods may be pointers in the method area. Direct reference to instance variables and instance methods is the offset from the beginning of the object image to the instance variables or method table.

The instance variables are organized as follows: from the Object class to the instance type, the instance variables declared in the class are placed in the Object image in order of appearance in the class file.

The instance method is organized in a similar way: from the Object class to the instance type, the instance method pointers declared in the class are placed in the Object image in sequence in the class file. However, the overwritten method will appear at the position corresponding to the superclass (the position where the method appears for the first time ).

However, the access interface method cannot be accessed simply by the offset of the method table. Instead, you must search for the method table of the object class to find the method.

For example, the Factory interface is implemented by A and B respectively to implement its produce () method. However, since A and B cannot guarantee that they are derived from the superclass that implements the Factory interface, the same produce () method offset means that the Factory's produce () method cannot be accessed through the offset of the method table.

7. Load Constraints

For a symbolic reference of a type pointing to another type, if the referenced type and the referenced type are not loaded by the same initial loader (which may be implemented by the user-defined ClassLoader ), the VM must ensure that the referenced types are consistent in different namespaces. In this way, after the custom ClassLoader is used to load the untrusted class type, it will not resolve the reference to the symbol of the referenced type, think of the trusted type as the untrusted type that has been parsed (because only the permission name and descriptor are used for symbol reference of the method, and the initial class loader cannot be known ), therefore, an untrusted method is called to access a protected member of the trusted type.

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.