1. Class loading and bytecode technology
1. Class file structure
2. Bytecode instructions
3. Compile time processing
4. Class loading phase
5. Class loader
6. Runtime optimization
1. Class file structure
Class file structure
1.1 Magic number magic
introduce
The first 4 bytes of each java class file are the magic number: 0x CAFEBABE . The function of the magic number is to distinguish between java class files and non-java class files.
1.2 Version minor_version, major_version
introduce
4~7 bytes, indicating class version 00 34 (hexadecimal converted to decimal is 52), indicating java8
51 java7
52 java8
53 java9
1.3 Constant pool
introduce
Store literals (text strings, final constants) and symbol references (package names, class names)
①8~9 bytes, the 2 bytes of the constant pool counter represent the length of the constant pool. 00 23 (decimal 35) means there are 1~34 items.
②The next n bytes (1-constant pool-1) are the constant pool table, which stores literals (strings, final constant values) and symbol references (classes, methods, field descriptors) .
1.4 Access identification and inheritance information
1.access_flags : The 2 characters after the end of the constant pool are the access flags
Determine whether this class is a class or an interface...
After looking up the table, we know that 00 21 is a combination of 0x0020+0x0001, so it is a public class
2.this_class : 2 bytes, what is the name of the current class?
00 03 represents the #3 item of the constant pool, which needs to be read from the constant pool.
3.super_class : 2 bytes, what is the name of the parent class.
00 04 represents the #4 item of the constant pool, which needs to be read from the constant pool.
4. Interface collection
①interfaces_count: 2 bytes, the number of implemented interfaces
00 00The interface is not implemented
②interfaces[interfaces_count]: 2 bytes, what are the interfaces?
1.5 Field table collection
fields
The field table represents the declared variables, the names of the fields, and the types of the fields. Constant description by reference to the constant pool
1.fields_count (field counter)
2 bytes, indicating the number of member variables
2.fields[ ] field table
Field table structure
Field table meaning
00 02: Access flag, look up the table above, private
00 05: Field name, check constant pool #5
00 06: Field data type, check constant pool #6
00 00: attribute counter
00 02: Property collection
1.6 Method table collection
methods:
① Points to a collection of constant pool indexes. Each method_info corresponds to information about a class or interface.
②The access modifier of the method, the return value of the method, and the method parameter information
1. methods_count method counter
2 bytes, the number of methods
00 02: There are 2 methods
2. methods[ ] method table
Store method information
detailed list
1.7 Additional attribute table collection
introduce
The source file name of the class file...