当前位置:网站首页>What is in the method area - class file, class file constant pool, runtime constant pool

What is in the method area - class file, class file constant pool, runtime constant pool

2022-06-26 10:13:00 ZZ learn java well

Recently, I have been troubled by something stored in the method area ?

       1. Stored in method area class Document information and class What is the relationship between file constant pool .

        2.class What is the relationship between file constant pool and runtime constant pool .        

        The method area stores the class information , Constants and static variables , That is, the data after the class is compiled . This statement is actually no problem , It's just too general . To be more specific, the method area stores the version of the class , Field , Method , Interfaces and constant pools . Literal and symbol references are stored in the constant pool .

        Symbol references include :1. The fully qualified name of the class ,2. Field name and properties ,3. Method name and properties .

        The following picture is the method area I drew ,class file information ,class Relationship between file constant pool and runtime constant pool

       

        The following diagram is used to represent the method area class What does the file information include :

       

          You can see in the method area class File information includes : magic number , Version number , Constant pool , class , Parent class and interface array , Field , Method and other information , In fact, the class also includes the information of fields and methods .

          The chart below is class The type of data stored in the file              

type name Number
u4magic1
u2minor_version1
u2major_version1
u2constant_pool_count1
cp_infoconstant_poolconstant_pool_count - 1
u2access_flags1
u2this_class1
u2super_class1
u2interfaces_count1
u2interfacesinterfaces_count
u2fields_count1
field_infofieldsfields_count
u2methods_count1
method_infomethodsmethods_count
u2attribute_count1
attribute_infoattributesattributes_count

        The following figure shows the contents stored in the constant pool :

         Picture description here

Use one class Actually decompile the file

Here is the original java Code

[java] view plain copy

  1. public class TestInt {    
  2.     private String str = "hello";    
  3.     void printInt(){    
  4.         System.out.println(65535);    
  5.     }    
  6. }   


After decompiling, you get class The file is as follows

You can see the decompiled class The contents of the document can correspond to the above . That's the answer class Document and class File constant pool relationship

class Relationship and difference between file constant pool and runtime constant pool

class The file constant pool stores when class The file is java Some literal and symbolic references stored in the method area after the virtual machine is loaded , Literal quantities include strings , Constants of basic types .

The runtime constant pool is when class After the file is loaded ,java Virtual opportunity will class The contents of the file constant pool are transferred to the runtime constant pool , stay class Some symbolic references of the file constant pool will be transformed into direct references , For example, static methods or private methods of a class , Instance construction method , Parent class method , This is because these methods cannot be overridden by other versions , So you can turn the symbol reference into a direct reference when loading , Some of the other methods turn a symbolic reference into a direct reference when the method is called for the first time .

summary :

The method area stores class File information and runtime constant pool ,class File information includes class information and class File constant pool .

The contents of the runtime constant pool are except class Outside the contents of the file constant pool , Will also class The symbolic reference in the file constant pool is transformed into a direct reference , Moreover, the contents of the constant pool can be added dynamically at runtime . For example, call String Of intern Method can string The value of is added to String In constant pool , here String The constant pool is contained in the runtime constant pool , But in jdk1.8 after , take String The constant pool is put in the heap .

原网站

版权声明
本文为[ZZ learn java well]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260922192843.html

随机推荐