Part of this paper is excerpted from 《 In depth understanding of Java Virtual machine Third Edition 》

summary

We know ,Java Cross platform , Its implementation is based on virtual machine and bytecode storage format .Java Not with virtual machines Java Language binding , Only with Class The file is associated with .Java Virtual machine as a universal 、 Machine independent execution platform , Any language can translate Java Virtual machines are the basis of their operation , With Class Documents serve as the delivery medium for their products .

Class Documents are a set of 8 Binary stream based on bytes , The data items are arranged in strict order in the file , There's no separator in the middle , This makes the whole Class The content stored in the file is almost all the necessary data for the program to run . When it comes to occupying 8 When there are more than bytes of data items , Then it will be divided into a number of 8 Bytes for storage .

Class There are two types of data in the file , They are non-conforming numbers and tables :

  • Unsigned numbers are basic data types , With u1、u2、u4、u8 To represent respectively 1 Bytes 、2 Bytes 、4 Bytes and 8 An unsigned number of bytes , Unsigned numbers can be used to describe numbers 、 Index reference 、 Quantity value or UTF-8 The encoding constitutes a string value
  • A table is a composite data type composed of multiple unsigned numbers or other tables as data items , General with _info ending . Tables are used to describe data in a hierarchical composite structure , Whole Class A document can also be seen as a table in essence

Whether it's an unsigned number or a table , When it is necessary to describe multiple data of the same type but with variable quantity , Often use the form of a front-end capacity counter plus several consecutive data items , In this case, a series of continuous data of a certain type is called a collection of a certain type .

Here is Class File format :

type name Number
u4 magic 1
u2 minor_version 1
u2 major_version 1
u2 constant_pool_count 1
cp_info constant_pool constant_pool_count - 1
u2 access_flags 1
u2 this_class 1
u2 super_class 1
u2 interfaces_count 1
u2 interfaces interfaces_count
u2 fields_count 1
field_info fields fields_count
u2 methods_count 1
method_info methods methods_count
u2 attribute_count 1
attribute_info attributes attributes_count

Magic sum Class Document version

Class Header of file 4 Bytes are called magic numbers (Magic Number), Its only function is to make sure that Class Whether the file can be accepted by the virtual machine , Its value is 0xCAFEBABE( Coffee baby ).

Next to the magic count 4 One byte stores Class Version number of the document : The first 5 And the 6 Byte is the minor version number (Minor Version), The first 7 And the 8 Byte is the major version number (Major Version).Java Version number from 45 Start , After each JDK When a large version is released, the main version number is added with 1. The high version of the JDK Can be downward compatible with previous versions of Class file , But you can't run later versions of Class file .

Constant pool

And then the Lord 、 The next version number is the constant pool entry , Constant pool entry needs to put an item u2 Data of type , Represents the constant pool capacity meter value (constant_pool_count), This capacity count is from 1 Not from 0 Start , The first 0 Items are used to express “ Don't reference any constant pool project ” The meaning of .Class Only the capacity count of the constant pool in the file structure is from 1 Start , Everything else comes from 0 Start .

There are two main types of constants in the constant pool : Literal and symbolic references . The amount of words is close to Java The concept of constant at the linguistic level , Such as text string 、 Be declared final Constant value of . And symbolic reference belongs to the concept of compilation principle , It mainly includes the following types of constants :

  • Packages exported or opened by modules (Package)
  • Fully qualified names of classes and interfaces (Fully Qualified Name)
  • Name and descriptor of the field (Descriptor)
  • The name and descriptor of the method
  • Method handle and method type (Method Handle、Method Type、Invoke Dynamic)
  • Dynamic call points and dynamic constants (Dynamically-Computed Call Site、Dynamically-Computed Constant)

Java Will load on the virtual machine Class File dynamic connection , Convert symbolic references to real memory entries . Every constant in the constant pool is a table , At first there was 11 Different data structures , Later, in order to better support dynamic language calls , additional 4 Constants related to dynamic languages , Later, in order to support Java modularization , Joined again 2 Constant , So the deadline JDK13, Constant table has 17 There are different types of constants . this 17 Class tables all have one thing in common , The first bit of the table structure is a u1 Flags of type (tag)

17 The specific meanings of the constant types are shown in the table :

type sign describe
CONSTANT_Utf8_info 1 UTF-8 Encoded string
CONSTANT_Integer_info 3 Integer literal
CONSTANT_Float_info 4 Floating point literal
CONSTANT_Long_info 5 Long integer literal quantity
CONSTANT_Double_info 6 Double precision floating point literal
CONSTANT_Class_info 7 A symbolic reference to a class or interface
CONSTANT_String_info 8 String type literal
CONSTANT_Fieldref_info 9 Symbol reference for field
CONSTANT_Methodref_info 10 Symbolic references to methods in class
CONSTANT_InterfaceMethodref_info 11 Symbolic references to methods in interfaces
CONSTANT_NameAndType_info 12 A partial symbol reference to a field or method
CONSTANT_MethodHandle_info 15 Represents a method handle
CONSTANT_MethodType_info 16 Represents the type of method
CONSTANT_Dynamic_info 17 Represents a dynamically computed constant
CONSTANT_InvokeDynamic_info 18 Represents a dynamic method call point
CONSTANT_Moudle_info 19 Represents a module
CONSTANT_Package_info 20 Represents an open or exported package in a module

Access signs

After the constant pool ends , The next 2 Bytes represent access flags (access_flags), This flag is used to identify access information at some class or interface level , Including this Class Class or interface ; Is it defined as public type ; Is it defined as abstract type ; If it's a class , Is it declared as final wait . And the specific meaning of the tag :

Logo name Flag value meaning
ACC_PUBLIC 0x0001 Is it Public type
ACC_FINAL 0x0010 Is it declared as final, Only classes can set
ACC_SUPER 0x0020 Is it allowed to use invokespecial New meaning of bytecode instruction
ACC_INTERFACE 0x0200 Mark this is an interface
ACC_ABSTRACT 0x0400 Is it abstract type , For interfaces or abstract classes , Secondary flag value is true , Other types are false
ACC_SYNTHETIC 0x1000 Flag this class is not generated by user code
ACC_ANNOTATION 0x2000 Mark this is an annotation
ACC_ENUM 0x4000 Flag this is an enumeration

access_flags There's a total of 16 Flags can be used , At present, only one of them is defined 9 individual , All unused flag bits are required to be zero .

Class index 、 Parent index and interface index set

Class index (this_class)、 Parent index (super_class) And interface index (interfaces) All in order after the access flag , Class index and parent index refer to two u2 The index value of type represents , And the interface index is a set of u2 A collection of data of type .

A class reference is used to determine the fully qualified name of the class , The parent class index determines the fully qualified name of the parent class of the class , because Java Multiple inheritance not allowed , So there's only one parent index ,Object The parent class index of class is 0. The class index and the parent index each point to a CONSTANT_Class_info Class descriptor constant for , Through this index value, we can find the definition in CONSTANT_Utf8_info A fully qualified name string in a constant of type .

For interface index sets , The first entry u2 The data of type is interface counter (interfaces_count), Indicates the capacity of the index table , If the class does not implement any interfaces , Then the value of the counter is 0, The index table of the subsequent interface no longer takes up any bytes .

Field table set method table set

Field table set (field_info) Used to describe variables declared in an interface or class , Including class level variables and instance level variables , But not including local variables declared inside methods . The field contains the scope of the field to be specified (public、private、protected)、 Instance variable or class variable (static)、 variability (final) wait . This information either has , Or not , It's very suitable to use flag bits to represent , And the field is called , Defined why data types , These are not fixed , Can only be described by constants in the constant pool .

The field table structure is as follows :

type name Number
u2 access_flags 1
u2 name_index 1
u2 descriptor_index 1
u2 attributes_count 1
attribute_info attributes attributes_count

access_flags Used to identify field modifiers (public、static、final、volatile ...),name_index and descriptor_index All references to constant pools , Represents the simple name of the field and the descriptor of the field and method respectively . After that is the property sheet collection , Used to store some extra information .

The field table collection does not list fields inherited from the parent class or interface , But it's possible that the original Java Fields not in the code , For example, in order to maintain the accessibility of internal classes to external classes , The compiler automatically adds fields to the external class instance .

The method table set is almost exactly the same as the field table set , Only among the options available for the flag and property sheet collection . As for the code in the method , After compilation, it is stored in the method property table collection with a name of Code In the properties of .

Class Class file structure

  1. 《 In depth understanding of Java virtual machine 》 Class file structure

    Review of the last section In the last section , It mainly introduces some of its own tuning or troubleshooting experience in its own working environment . The so-called variety is inseparable from its origin , This case is our way to solve the problem . This section focuses on In the previous chapters , We have a macro view of virtual machines ...

  2. In depth understanding of java virtual machine 【Java Class Class file structure 】

    Java Language claims to be written once from its birth , Cross platform features that run everywhere , Its implementation principle is that the source code file is not directly compiled into machine instructions , It's compiled as Java Bytecode files that virtual machines can recognize and run (Class Class file ,*.class), Bytecode ...

  3. ( turn )《 In depth understanding of java virtual machine 》 Learning notes 5——Java Class Class file structure

    Java Language claims to be written once from its birth , Cross platform features that run everywhere , Its implementation principle is that the source code file is not directly compiled into machine instructions , It's compiled as Java Bytecode files that virtual machines can recognize and run (Class Class file ,*.class), Bytecode ...

  4. Class Class file structure 、 Class loading mechanism and bytecode execution

    One .Class Class file structure Class Class files are arranged in strict order , It consists of an unsigned number and a table , A table is a data structure consisting of multiple unsigned numbers or other data items . Class Class file formats are arranged in the following order :   type name Number u ...

  5. turn : thorough Java virtual machine 】 The second :Class Class file structure

    Reprint please indicate the source :http://blog.csdn.net/ns_code/article/details/17675609 Platform independence Java It's a platform independent language , Thanks to Java Source code compiled after the generation of memory ...

  6. 《 In depth understanding of java virtual machine 》 note —— analysis java Class file structure

    It's not clear all the time jvm How to load a class , In the process of reading materials, I feel confused . Before understanding class loading , First look at it. java What is the class file structure of , What's in it . The most direct reference, of course, is the official document :The Java ...

  7. JVM note 9-Class Class file structure

    1.Class Class file structure   Class Documents are a set of 8 Bit byte is the base unit of binary stream , The data items are arranged in strict order and compactly in Class In the document , There's no separator in the middle , This makes the whole Class In file ...

  8. 《 In depth understanding of Java virtual machine 》----- The first 6 Chapter Class file structure ——Java Advanced development must understand

    The result of code compilation changes from local machine code to bytecode , It's a small step in the development of storage formats , It's a big step in the development of programming languages . 6.1 summary I remember in the first computer program class my teacher said :“ Computers only know 0 and 1, So the program we write needs to be translated by the compiler ...

  9. In depth understanding of Java virtual machine ( Class file structure + Class loading mechanism + Bytecode execution engine )

    Catalog 1. Class file structure 1.1 Class Class file structure 1.2 Magic number and Class Version of a file 1.3 Constant pool 1.4 Access signs 1.5 Class index . Parent index and interface index collection 1.6 Field table set 1.7 Method set 1 ...

  10. In depth understanding of JVM( 6、 ... and ) Class file structure

    6.1 About class files 1.class A compilation of a file , Cross platform running everywhere : 2.JVM It's not just cross platform , And cross language , Whether it's JRuby still Groovy Written procedures , As long as it's compiled in line with JVM canonical class The document ...

Random recommendation

  1. cout Format output

    Problem description : Yes N A rope , Their lengths are Li. If you cut it out of them K The same rope , this K How long can each rope be ? ( remarks : Keep the answer to two decimal places ) <1> Accurate to two decimal places output #inclu ...

  2. Cocos2d-JS Replace the initialization scenario

    Cocos2d-js The default startup entry of the project is app.js, Prepare to change to another entry file, such as :GameScene.js var GameLayer = cc.Layer.extend({ ctor:functio ...

  3. solve mysql&quot;Access denied for user&#39;root&#39;@&#39;IP Address &#39;&quot; problem

    Use... On another server MySQL-Front When linking : resolvent : stay MySQL On the server root After logging in , The implementation is as follows SQL sentence : mysql Login command : >mysql -u root -p; Then hold ...

  4. Android And Learning route

    The first stage :Java object-oriented programming 1.Java Basic data types and expressions , Branch loop .  2.String and StringBuffer Use . Regular expressions .  3. Object oriented abstraction , encapsulation , Inherit , polymorphic , Classes and objects , To Xiangchu ...

  5. JS Some problems in study

    1. Find out all the daffodils : Narcissus number, for example :153 Characteristics : 1^3+5^3+3^=153 And the number of daffodils will only be three digits , So you can solve the problem in a circular way , The cycle condition can be set to : var i = 1;i < ...

  6. appium Commonly used API Use summary !

    Summarize the application of common functions , Later in the process of using the direct search call can be Get interface properties . Control properties 1.current_activity: obtain activity name device.current_activity 2. ...

  7. Web Frequently asked questions about security development

    Reprinted from : http://blog.csdn.net/fengyinchao/article/details/50775121 Not all Web Developers have the concept of security , Maybe even some security vulnerabilities have never been heard of ...

  8. win7 System Oracle Local database backup

    First step : Command line login sqlplus sys/root@orcl as sysdba sqlplus Super administrator / password @ Data objects as sysdba The second step : establish DIRECTORY create dir ...

  9. PAT 1001 A+B Problem solving report

    PAT 1001 A+B Code link : Portal Topic link : Portal A brief introduction to the topic : Given two values a,b: Range -1000000 <= a, b <= 1000000: Output in the specified format a+b Result , example : ...

  10. js Control page Jump 5 Methods

    js Control page Jump 5 Methods Programming navigation : Click jump route , It's called programming navigation , use js Write code to jump . History yes bom Medium History.back It's a page back Histiory.go(1) One page ahead Hi ...