1 class files vs. Virtual machines:
The Java virtual machine is not bound to any language, including Java, and is associated only with a specific binary file format such as the class file, which contains the Java Virtual machine instruction set and symbol table, as well as several other ancillary information. For security reasons, the Java Virtual Machine specification requires many mandatory syntax and structural constraints in the class file, but any language can be represented as a valid class file that can be accepted by a Java virtual machine. Java Virtual machines don't care about the source of the class.
Structure of the 2 class file
The class file is a set of 8-byte-based binary streams, each of which is tightly arranged in a class file, with no delimiters in the middle, which makes the entire class file stored in almost all the necessary data for the program to run, with no gaps present.
Class structure is not like XML and other descriptive languages, because there is no delimiter, so the order and length are not allowed to change.
2.1 Class file version
The first four bytes of each class file are called Magic Numbers, and its sole purpose is to determine if the file is a class file that can be accepted by the virtual machine. Many file storage standards are identified by the magic number instead of the extension, and the pedagogical gardening tablet format is used. Use magic numbers instead of extensions to identify primarily security-based considerations, because filenames can be arbitrarily altered.
The four bytes of the magic number are stored with the version number of the class file: the 5th and 6th bytes are the minor version number, and the 7th and 8th bytes are the major version numbers. Java has a version number starting from 45, and a higher version of the JDK can be backwards compatible with the previous version of the class file, but not the later version of the file.
2.2 Constant Pool
followed by the main version number is the constant pool entrance, the constant pool can be understood as a class file in the repository, it is the class file structure with the most associated with other projects of the data type, is the largest class file space to occupy one of the data items, It is also the first table-type data item in the class file that appears.
Because the number in the constant pool is not fixed, a U2 type of data is placed at the entrance of the constant pool, representing the constant pool count value, which is counted as 1 instead of 0. The capacity count of only the constant pool in the class file structure starts at 1, and for other collection types, including the interface index collection, the Field table collection, the Method table collection, and so on, the capacity count is the same as the general habit, starting from 0.
A constant pool holds two major classes of constants: Literal constants and symbolic references. Literal constants are similar to the concept of constants at the Java language level, such as text strings, declared final constants, and so on. The symbolic reference to the constants is a compilation principle concept, including the following three types of constants:
Permission naming for classes and interfaces
Name and descriptor of the field
The name and descriptor of the method
When the Java code is Javac compiled, it is dynamically connected when the virtual machine loads the class file. That is, the final memory layout information for each method and field is not saved in the class file, so these fields, the method's symbolic reference does not get a true memory entry address without running the transition, and cannot be used directly by the virtual machine.
For the source code as follows:
public class Classstudy {private static final int ss=22;public static void main (string[] args) {System.out.println (ss);}}
JAVAP output constant table with the disassembly tool from the JDK
2.3 Access Flags
After the constant pool ends, the next two bytes represent the access flags, which are used to identify some class or interface-level information, including whether the class is classes or interfaces, is a public type, is defined as an abstract type, and if it is a class, whether it is declared final, and so on.
A collection of 2.3-class indexes, parent-class indexes, and interface indexes
Both the class index and the parent class index are a U2 type of data, and the interface index is a set of U2 types of data, and the class file has these three data to determine the inheritance of this category. The class index is used to determine this tired fully qualified name, and the parent class index is used to determine the permission naming of the parent class of the class.
The class index, the parent class index, and the interface index collection are arranged sequentially after the access flag, and the class index and the parent class index are represented by the index values of two U2 types.
For the interface index collection, the first entry of the entry U2 type counter, which represents the capacity of the Index table. If the class does not have any interfaces, the counter value is 0, and the subsequent index does not occupy any bytes.
2.4 Field table Collection
A field table is used to describe an interface or a variable declared in a class. Fields include class-level variables and instance variables, but do not include local variables inside the method.
Java class file structure