Java-based Class files that run everywhere

Source: Internet
Author: User

Java implements a compile-time-running foundation, from which the Java virtual machine masks the underlying details of the operating system. Use the class file to store the compiled source program, which causes the Java program's compilation to be decoupled from the operating system. Because Java class files are designed to be decoupled from the Java language, the Java language Specification and the Java Virtual Machine specification are released, allowing other languages such as Scala, Groovy, JRuby, Jpython, and other Java-based virtual machine languages to be in the class file format The generated class file can also be run on the virtual machine.

class file format

The class file uses the following structure to store the binary content. Where U2, U4, respectively, occupy 2, 4 bytes.

{

U4 Magic; Magic number, fixed to 0xCAFEBABE

U2 minor_version; Minor version number

U2 major_version; Major Version number

U2 Constant_pool_count; Constant Pool counter

Cp_info Constant_pool[constant_pool_count-1]; Constant pool

U2 access_flags; Access flags, declaring permissions

U2 This_class; Class Index

U2 Super_class; Parent Class Index

U2 Interfaces_count; Number of interfaces

U2 Interfaces[interfaces_count]; Interface List

U2 Fields_count; Number of fields

Field_info fields[fields_count];//Field List

U2 Methods_count; Number of methods

Method_info Methods[methods_count]; Method List

U2 Attributes_count; Number of attributes

Attribute_info Attributes[attributes_count]; Property List

}

class file Content Interpretation

Chang:

All string constants, class names, interface names, field names, and other constants that are used to store class files. Other items in a class file tend to reference constants in a constant pool, so the constant pool capacity count starts at 1 and 0 is used to indicate that no other project references a constant pool. In a constant pool, there are two major types of constants, literal and symbolic, which are mainly strings, final type constant values, etc., and symbolic references include the fully qualified name of the class and interface, the name and description of the field, and the name and descriptor of the method. The symbolic reference in the previous "Java virtual machine class loading mechanism" that is referred to as a symbol reference in a direct reference is a symbolic reference in a constant pool.

Access Flags:

The access information for a class or interface, including the public, final, super, interface, abstract, annotation, enum properties, and code generated using synthetic to represent non-Java sources.

Class Index:

This_class stores an index in a constant pool, and the constant at the index represents the class or interface defined by the class file. If this is a class, Super_class is 0 or stores an index in a constant pool, the constant at the index represents the parent class, and if this is an interface, Super_class stores an index in the constant pool, and the constant at the index must be java.lang.Object. The fully qualified name of the current class can be determined by this_class, and the fully qualified name of the parent class can be determined by super_class.

Interface list:

If this is a class, store the list of interfaces that the class implements, stored in the order of the interfaces after implements, or, if this is an interface, stores all the parent interface lists for that interface, stored in the order of the interfaces after extends.

Field List:

A variable that stores a class or interface declaration, including class variables and instance variables. Describes the information for each variable, including scope, static, final, volatile, transient, type, name, and so on. Where the name and type of the field need to refer to constants in the constant pool to describe.

List of methods:

A method that stores a class or interface declaration, including class methods and instance methods. Describes the information for each method, including access flags, name indexes, descriptor indexes, attribute table collections, and so on. Here only the information of the method is stored, and the implementation code of the method is compiled into bytecode and stored in the "code" attribute in the attribute table collection.

Property list: The virtual machine specification defines a large number of attributes, and class files, field lists, and method lists can all use attributes to describe proprietary information. The name of the property needs to refer to the represented representation of the constant pool. The code in the method body is compiled and stored in a property named code.

Summarize

After compiling the Java source program to generate a class file instead of a binary executable, through the Java Virtual machine to parse and execute the program in the class file, the implementation of "compile once, run everywhere." In a class file, basic information about a class or interface is stored, such as the version number, class name, interface list, field list, method list, and so on.

Original address: Java is running around the basic Class file

Java-based Class files that run everywhere

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.