Deep understanding of file structures such as Java virtual machines

Source: Internet
Author: User

As we all know, Java is a platform-agnostic language. Java code compiles. java files into bytecode, or. class files, through a Java compiler, such as Javac. The bytecode is running on top of the JVM virtual machine. Different platforms have different virtual machine mapping rules. So, from the top level of the virtual machine, Java is platform agnostic. Write once, a Java program written as long as it is on a JRE-equipped computer, or any device, can run, that is, run anywhere.

Here's a look at the class file structure you learned from the in-depth understanding of Java virtual Machines, which is class file specification.

class file, according to the following data items, whether in order or quantity or even the data stored in the byte order (big-endian) is strictly defined, which byte represents what meaning length sequence can not change. U1 U2 U4 U8 represents a 1 2 48-byte unsigned number that can be used to describe a numeric index reference, a quantity value, or a string value that is composed of UTF-8.

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

For the above structure, there is a lot of content for each info. See more in-depth Java virtual machine ....

Let's talk about how the Java virtual machine is mixed with the class file and what happens after the class file is loaded into the virtual machine.

Java Virtual machine class loading mechanism: the virtual machine loads the data describing the class from the class file into memory, then verifies the data, transforms the parsing, initializes it, and eventually forms the Java type that can be directly exploited by the virtual machine.

Java loading links and initialization are done when the program is running.

The life cycle of a class: load-Validate-prepare-parse-initialize--use--uninstall.

Under what circumstances will the initialization be triggered?

1.new (create instance Directive) Getstatic Putstatic Invokestatic (Accesses a class field, or becomes a class variable): Reads or sets a static field when using new to instantiate an object ( Except for static fields that have been put into the constant pool by the final modification at compile time , and when static methods are called

2. Use the Java.lang.reflect package to make reflection calls to the class. If the class is not initialized, the initialization is triggered first.

3. When initializing a class, the initialization of its parent class is triggered first if the parent class has not yet been initialized.

4. When a virtual machine is started, the user needs to specify a main class that needs to be executed (that is, the class of Main ()) to initialize the main class first.

5.jdk1.7 above, dynamic language support, if a java.lang.invoke.MethodHandle instance of the final result ref_getstatic,ref_putstatic,ref_invokestatic the method handle, and the corresponding class for this method handle has not been initialized, it must be triggered.

The virtual machine specification indicates that only the above behavior is initialized, called an active reference. In addition to any method of referencing a class, it does not trigger initialization, which is called a passive reference.

In fact, according to the above five rules, you can know the load order of the class.

Deep understanding of file structures such as Java virtual machines

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.