Deep understanding of Java Virtual Machine reading notes (III.)

Source: Internet
Author: User

Java is a platform-independent language, and the cornerstone of platform independence is byte code. In fact, the virtual machine does not care about the origin of class is what language,

Can run on a Java virtual machine as long as it conforms to the structure of the class file

The file is a set of binary streams with eight-bit bytes, with no delimiters in the middle. Where the constant pool is the class file structure in relation to other items

The most data type, and one of the most important items in the class file space

There are two main types of constants in a constant pool:

A, literal: such as a literal string, a constant value declared as final

B, symbolic Reference: include the fully qualified name of the class and interface, the name and descriptor of the field, the name and descriptor of the method

When a virtual runtime needs to obtain the corresponding symbolic reference from Chang, and then parse and translate the class when it is created or run

To a specific memory address.

The language feature that Java can dynamically expand is the feature that relies on dynamic loading and dynamic linking during runtime.

Now, classes are included throughout the lifecycle: loading, connecting (validating, preparing, parsing three phases called connections),

Initialize, use, and uninstall seven phases

In addition to the parsing phase, the rest of the order of the phases is determined, and class loading must be in this order by department

"Start", note that it is not performed or completed in a step-by-step manner, as these phases are usually mixed into each other

Line, you typically invoke or activate another phase during the execution of one phase, and the parsing phase

You can start at the beginning of the initialization phase to support run-time binding of the Java language, or to dynamically bind

Note that the virtual machine specification strictly stipulates that the class must be initialized immediately in only four cases:

A. When instantiating an object using the New keyword, when reading or setting a static field for a class

And when a static method of a class is invoked (except for static fields, which are final decorated, where results are placed in the calling class's constant pool during compilation), as described in the following procedure.

B. When initializing a class, if it is found that its parent class is not initialized, the initialization of its parent class is triggered (but when an interface is initialized, the parent interface is not required to initialize all, and is initialized only when the parent interface is actually used).

C. When a reflection call is made to a class using the Java.lang.reflect package method, it is necessary to trigger initialization if there is no initialization

D. When a virtual machine is started, the user needs to specify a main class to execute (the class that contains the main method), and the virtual opportunity initializes the class first

The behavior in these four scenarios is called the active reference to a class, except that all references to classes do not trigger initialization, called passive references

Note that for static locations, referencing the static fields of the parent class by subclasses does not trigger initialization of the subclass

public class Superclass {

static{

System.out.println ("Super init");

}

public static int value = 123;

}

***************************

public class subclass extends superclass{

static{

System.out.println ("Sub init");

}

public static void Main (String args[]) {

System.out.print (Subclass.value);

}

}

For this program, the output will only be superinit, because for static fields, only the class that directly defines the field is initialized.

Scenario One: Because constants are stored in the constant pool of the calling class at compile time, there is no reference to the class that defines the constants, so it does not trigger the initialization of the class that defines the constants, look at the following code

Publicclass a{

Static {

System.out.println ("Constclass init");

}

publicstaticfinal String str = "Hello";

}

Publicclass B

publicstaticvoid Main (string[] args) {

System.out.println (A.STR);

}

}

The code output is Hello, although STR is defined in Class A, but in the compile phase the value "Hello" of this constant is deposited in a constant pool that calls Class B, and a reference to the constant a.str is actually converted to a reference to its own constant pool by the B class.

Scenario Two: Of course, the following code will first output Constclass init

Hello

public class a{

Static {

System.out.println ("Constclass init");

}

publicstaticfinal String str = "Hello";

publicstaticvoid Main (string[] args) {

System.out.println (A.STR);

}

}

A. Why? Because now Class A contains the main method, according to the preceding four kinds of cases that must be initialized, D, when the virtual machine is started, the user needs to specify a main class to execute (the class that contains the main method), and the virtual opportunity initializes the class first, which means that a has been initialized before the main method is invoked , the statement of the static statement block is output

Related Article

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.