Home > Java > javaTutorial > Getting Started with Java: Detailed Explanation of Java Class Files

Getting Started with Java: Detailed Explanation of Java Class Files

黄舟
Release: 2016-12-17 11:22:20
Original
1572 people have browsed it

1. What is a java class file?
A Java class file is a binary representation of a Java program. Each class file represents a class or interface. It is not possible to put multiple classes or interfaces in one class file. This allows the class file to be executed on any host no matter which platform it is generated on.

Although class files are part of the Java architecture, they are not inseparable from the Java language. You can compile programs in other languages ​​into class files, and you can compile Java program files into other binary forms.
Java class file is a binary stream based on 8-bit bytes. Data blocks are stored in sequential, delimiter-free, big-endian format.
2. Contents of class files
Java class files contain all information about classes and interfaces required by the Java virtual machine. The information in all class files is stored in the following four basic types:
    Table 6-1.
The main parts in the class file are stored in the order of Table 6-2:
Table 6-2. Format of a ClassFile Table
Type& #9;Name Count

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 attributes_count 1
attribute_info attributes_count
1. Magic encoding (magic)
The first four bytes of every Java class file are magic encoding (OxCAFEBABE). Class files can be easily identified through magic encoding.
2. Minor_version and major_version (minor_version and major_version)
The remaining four bytes are the minor version number and major version number. But as Java technology evolves, some new features may be added to class files. Every time the class file format changes, the version number will change accordingly. The virtual machine uses the version number to identify the class files it can process. The Java virtual machine can often only handle a given major version number and some minor version numbers below it. The virtual machine must reject class files that are no longer in scope for processing.
3. Constant_pool_count and constant_pool
Next is the constant pool. The constant pool contains constants that have been accessed by classes or interfaces, such as: strings, constants (final variable values), classes Name, method name. The constant pool is stored as a list. The number of constants in the list is the previously saved "constant_pool_count".
Many constants in the constant pool refer to other constants in the constant pool, and those references to constant pool constants will eventually be converted into direct references to the constants in the constant pool. Although the index in the constant list starts from 1, the number of constants still includes 0. For example, if there are 15 constants in a constant list, then its number of constants will be 16.
Every constant will have a flag at the beginning to indicate its type. When the virtual machine reads this flag, it will know the specific type of this constant. Table 6-3 lists these flags:
Table 6-3. Constant pool tags
Entry Type Tag Value Description
CONSTANT_Utf8 1 A UTF-8 encoded Unicode string
CONSTANT_Integer 3 An int literal value
CONSTANT_Float 4 A float literal value
CONSTANT_Long 5 A long literal value
CONSTANT_Double 6 A double literal value CONSTANT_Class 7 A symbolic reference to a class or interface
CONSTANT_String 8 A String literal value CONSTANT_Fieldref 9 A symbolic reference to a field
CONSTANT_Methodref 10 A symbolic reference to a method declared in a class CONSTANT_NameAndType 12 Part of a symbolic reference to a field or method
Each flag in Table 6-3 will have a corresponding table to describe some specific information represented by this flag. These corresponding flags will end with the flag name +_INFO. For example, the CONSTANT_CLASS flag corresponds to CONSTANT_CLASS_INFO.
The constant pool plays a very important role in the dynamic linking of programs. In addition to the various constant values ​​mentioned above, the constant pool contains the following three types of symbol references: full names of classes and interfaces, field names and descriptors, and method names and descriptors. A field is an instance or class variable in a class or interface, and the field descriptor is the type of the field. The method descriptor is the number, order and type of the method and return value and parameters. These full names are used by the virtual machine when linking this class or interface to other classes or interfaces. Because the class file does not contain any information about the memory structure, this link can only exist in the form of a symbolic reference. The virtual machine converts these symbolic references into actual addresses during execution. For specific information, see Chapter 8 "The Linking Model".
4. Access flags (access_flags)
The two bytes immediately following the constant pool are access flags, which represent several aspects of information about this class or interface. It has the following values:
Table 6-4. Flag bits in the access_flags item of ClassFile tables
Flag Name Value Meaning if Set Set By
ACC_PUBLIC 0x0001 Type is public Classes and interfaces
ACC_FINAL 0x0010 Class is final Classes only
ACC_SUPER 0 x0020 Use new invokespecial semanticsClasses and interfaces
ACC_INTERFACE 0x0200 Type is an interface, not a class All interfaces, no classes
ACC_ABSTRACT 0x0400 Type is abstract All interfaces, some classes
The ACC_SUPER flag is for compatibility with the old SUN compiler. All unused access flags must be set to 0.
5. Class name (this_class)
The next two bytes store the index of a constant pool. Entities in this constant pool must be of type CONSTANT_CLSS_INTO, which contains flag and name indexes. The flag is CONSTATN_CLASS, and the name index should be an index of type CONSTANT_UTF8_INFO that stores the full name of the class or interface.
6. Parent class (super_class)
After this_class is the two-byte super_class. It is also an index of a constant pool, which stores the full name of the parent class. It is processed the same as this_class. When the parent class is java.lang.Object, super_class should be 0. For interface super_class are all 0.
7. (interfaces_count and interfaces)
The number of parent interfaces is saved in interfaces_count, and the index of some constant pools is saved in the form of an array in interfaces. Each index points to a CONSTANT_CLASS_INFO constant, which stores the full name of each parent interface. The order of this array is the order from left to right in which the parent interface appears in the implements and extends statements.
8. (fields_count and fields)
The fields are saved in a list of field_info, and fields_count is the length of this list. Only the variables declared in the class or interface are saved in the Field_info list. Fields inherited from the parent class or parent interface are not saved here.
Each entry in the field_info table describes the information of a field, including: field name, descriptor, and access permissions. If a field is declared final, then the information about this field will be saved in the field_info table and also in the constant pool.
9. (methods_count and methods)
Method information is stored in the method_info table, mehtods_count is the length of the table. The Method_info table only stores methods declared in classes or interfaces, and does not store methods inherited from parent classes or interfaces.
The method name and descriptor (return value and parameter type) are saved in the Method_info table. If it is not an abstract method, the size of the stack (used to save local variables), the maximum size of the operand stack, the caught exception list, the bytecode of the method, the optional line number and the local variable table are also saved. If the method throws some checked exceptions, method_info also contains a list of checked exceptions.
10. (attributes_count and attributes)
The last thing in the class file is the number of attributes and the attribute_info list. The Atribute_info table stores some indexes pointing to constant_utf8_info in the constant pool, which stores the name of the attribute. Two types of attributes are defined in the Java Virtual Machine Specification: source code and inner classes.

The above is the introduction to Java: detailed explanation of java class files. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template