Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse Java constant pool and string intern

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to parse the Java constant pool and the string intern. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

When the Java application is running, the Java virtual machine holds an internal pool of runtime constants, which is different from the constant pool of class files and is a data structure that maps the constant pool of class files to the virtual machine.

1.CONSTANT_Class entry parsing

The symbolic parsing of array classes is special. If it is an array of base types, the virtual machine will create a new array class of that base type and create an instance of Class to represent that type. The defined class loader of the array class is the startup class loader. If it is an array of reference types, then the reference type will be resolved before that, and the definition class loader of the array class is the definition class loader of the reference type.

Parsing of non-array classes and interfaces goes through the following steps:

(1)。 Load the type and all its supertypes

If the type has been loaded into the current namespace of the virtual machine before then, you can simply use the type that has already been loaded, otherwise it will be loaded by the initial class loader of the referenced originating class. The loading of the superclass of the target type must be carried out on the basis of loading the current type, because only after loading the current type can we find the symbol reference of the direct superclass from the super_ class field of the class file, and then recursively parse and load until the java.lang.Object class. During the recursive return, the interfaces domain is checked to see which interfaces are implemented or extended, and the symbolic reference to the interface is recursively traversed again.

(2)。 Check access permissions

This is followed by the connection and initialization of the target type so that the type can be used properly. As mentioned earlier, initialization of the target type requires that all of its superclasses must be initialized (hyperinterfaces are not required), and because its superclass has been loaded, it is no longer dependent on the parsing order from that class to the Object class, but from the Object class to the class. The connection and initialization steps for the type are as follows:

(3)。 Type check

(4)。 Type preparation

(5)。 Type resolution (deferred)

Note that this process is the resolution of symbolic references to referenced types and their superclasses, because some symbolic references to referenced types are not immediately used, so this step was strictly a process of symbolic resolution of the type that initiated the reference. These symbolic references are resolved, loaded, connected, and initialized only when the types pointed to by these symbolic references of the referenced types are actively used.

(6)。 Type initialization

2.CONSTANT_Fieldref entry parsing

Since a type will not contain the field defined by its supertype, the search for the target field will start with the type that the field points to, start with that type, recursively search for its implemented or extended interface, and then recursively search for its superclass. until the target field is found, and the field entry of the running constant pool is marked as parsed And change the data of the constant pool to a direct reference to this field.

3.CONSTANT_Methodref entry parsing

Similar to but different from the search for a field, the search order starts with that type, then recursively searches for its superclass, and recursively searches for the interfaces it implements or extends.

4.CONSTANT_InterfaceMethodRef entry parsing

The search for the interface method starts with the parsed interface and searches its hyperinterface recursively.

5.CONSTANT_String entry parsing

The Java virtual machine maintains the string as a string object, while the virtual machine maintains a string pool that contains references to all string objects that are "detained". To parse the CONSTANT_String constant pool, we should first check whether the reference to the string object exists in the string pool. If so, we directly parse the constant pool data into a reference to the string object. If it does not exist, we need to create a string object based on the string sequence, add its reference to the string pool, and parse the constant pool data to the reference.

You can also use the intern object of the String object to detain 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 return the reference directly, otherwise, the string will be detained, but note that the string object reference returned by the detention will not point to the original String object, because the original String object is in the Java heap The object of the string pool is created by the virtual machine and maintained by the virtual machine.

Package com.ice.intern; public class InternTest {public static void main (String args []) {String a = new String; String b = a; String c = new String ("123");; System.out.println ("before intern:"); System.out.println ("a = b?:" + (a = b)) System.out.println ("a = c?:" + (a = c)); a = a.intern (); c = c.intern (); System.out.println ("after intern:"); System.out.println ("a = b?:" + (a = b)); System.out.println ("a = c:" + (a = = c);}}

The results are as follows:

(6)。 Entry parsing of other types (basic data types)

You can use the constant values contained in the constant pool directly.

6. Direct reference

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

Instance variables are organized from the Object class to the type of the instance, and the instance variables declared in the class are placed in the object image in the order in which they appear in the class file.

Instance methods are organized similarly: starting from the Object class to the type of the instance, the instance method pointers declared in the class are placed in the object image in the order in which they appear in the class file. However, the overridden method will appear in the location corresponding to the superclass (where the method occurs for the first time).

However, the access interface method can not be accessed simply through the offset of the method table, but must search the method table of the object's class to find the method.

For example, the produce () method of the Factory interface is implemented by An and B respectively, but because An and B cannot guarantee that they are derived from the same superclass that implements the Factory interface, that is, they have the same produce () method offset, it is impossible to access the Factory produce () method through the offset of the method table.

7. Loading constraint

For symbolic references where one type points to another, if the referenced type and the referenced type are not loaded by the same initial loader (possibly through a user-defined ClassLoader), the virtual machine must ensure that the referenced types are consistent in different namespaces. In this way, after loading the untrusted class type through the custom ClassLoader, when parsing the symbolic reference to the referenced type does not occur, the trusted type is treated as an untrusted type that has been parsed (because the symbolic reference to the method has only permission names and descriptors, and its initial class loader is not and cannot be known), thus the method of the untrusted type is called to access the protected members of the trusted type.

The above is how to parse the Java constant pool and the string intern that the editor shared for you. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report