Java code running mechanism

Source: Internet
Author: User

Java code compilation becomes a platform-independent bytecode (that is, a class file). Of course, this bytecode is not executable, and the Java interpreter must be used to interpret the execution. The Java Virtual machine, the JVM, is responsible for interpreting the execution of bytecode files . The JVM is a virtual machine that can run Java bytecode files. The JVM on all platforms provides the same programming interface to the compiler, and the compiler only needs to target the virtual machine, generate the code that the virtual machine understands, and then interpret the execution by the virtual machine. In some implementations of virtual machines, virtual machine code is also converted into machine code execution for a particular system, thus improving execution efficiency.

when we call when the Java command runs a java program, the command launches a java virtual machine process, regardless of the How complex the Java program is, how many threads the program starts, and all of them in the Java virtual machine process. All threads of the same JVM, all variables are in the same process, and they all use the Memory area of the JVM process.

When the program actively uses a class, if the class has not been loaded into memory, the system will be loaded, connected, initialized three steps to initialize the class, if there is no accident, theJVM will complete the three steps, so sometimes the three steps are collectively referred to as class loading or class initialization.

1. Loading

Class loading refers to reading class files into memory and creating a Java.lang.Class object for them, which means that when any class is used in a program, the system creates a Java.lang.Class object ( class, They are actually objects, both of which are Examples of java.lang.Class.

Classes are loaded by the class loader, which is typically provided by the JVM, which is also the basis for all of our previous programs to run, andthese classloader are often referred to as the System class loader. In addition, developers can create their own class loaders by integrating the ClassLoader base class.

The class loader typically does not have to wait until the class is first used , and theJava Virtual Machine specification allows the system to preload some classes.

2. Connect

When the class is loaded, the system generates a corresponding class object, which is then entered into the connection phase, and the connection phase is responsible for merging the class's binary data into the JRE.

Class connection can be divided into the following three stages:

(1) Verification

Verify that the class being loaded has the correct internal structure and is consistent with other classes

(2) Prepare

Allocates memory for static properties of a class and sets default initial values

(3) Analysis

To replace a symbolic reference in the binary data of a class with a direct reference

3. Initialize

During the initialization phase of the class, the virtual machine is responsible for initializing the class, primarily to initialize the static properties.

There are two ways to specify an initial value for a static property in a Java class:

(1) specifying the initial value when declaring a static property;

(2) Use static initialization blocks to specify initial values for static properties. If you specify an initial value for the same static property in both of these ways, the JVM executes the statements sequentially in the order in which they are arranged in theprogram.

The JVM Initializes a class that consists of the following steps:

(1) If the class has not been loaded and connected, the program loads and joins the class first.

(2) If the immediate parent class of the class has not been initialized, its parent class is initialized first.

(3) If there are initialization statements in the class, the system executes the initialization statements sequentially.

The time of class initialization

When a Java program uses a class or interface for the first time in the following 6 ways, the system initializes the class or interface:

(1) Creating an instance of a class

(2) calling a static method of a class

(3) Accessing a static property of a class or interface, or assigning a value to the static property

(4) Use reflection to force the creation of a class or interface corresponding to the Java.lang.Class object

(5) initialize subclasses of a class

(6) directly using the java.exe command to run a main class, when running a main class, the program will first initialize the main class

In addition, there are several situations that need to be specifically noted:

For a final static property, if the property can get a property value at compile time, you can assume that the property can be used as a compile constant. When a program uses compile-time constants, the system considers this to be passive use of the class, so it does not cause initialization of the class.

Conversely, if the value of a static property of the final type cannot be obtained at compile time, it must wait until run time to determine the value of the property, and if the static property is accessed through that class, it can be assumed that the class is used for active access, which will cause it to be initialized.

Java code running mechanism

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.