Java Re-learning-JVM class loading and execution mechanism

Source: Internet
Author: User

about the JVM


The JVM is the abbreviation for Java Virtual machine (Java VM), which is a specification for computing devices, a fictional computer that is implemented by simulating various computer functions on a real computer. A Java Virtual machine consists of a set of bytecode instructions, a set of registers, a stack, a garbage-collected heap, and a storage method domain. The JVM masks information related to the operating system platform, enabling Java programs to run without modification on multiple platforms by generating only the target code (bytecode) that runs on the Java virtual machine. When the JVM executes the bytecode, it actually eventually interprets the bytecode as a machine instruction execution on a specific platform.


Loading of the JVM class

The Java Virtual machine is loaded, connected, initialized.

Java program of the use of the class, divided into two, one is active use, the second is passive use, all Java Virtual machine implementation must be in each class or interface by the Java program " first active use " when the class is initialized.


Active use includes six types:

-Create an instance of the class

-Access static variables for a class or interface, or assign a value to the static variable

-static method of calling class

-Reflections (e.g. Class.forName ("Com.tgb.java"))

-Initializes a subclass of a class

-java class that is indicated as the startup class when the virtual machine is started

Class loading, perhaps very familiar, at the beginning of learning rectification C language, It is known that the binary data in the class's. class file is read into memory, placed in the method area of the run-time data area, and a Java.lang.Class object is created in the heap to encapsulate the data structure of the class within the method area.


how the class is loaded:

-Load directly from the local system

-Download. class files over the network

-Load. class files from archive files such as Zip,jar

-Extract the. class file from the proprietary database

-Dynamically compile Java source files into a. class file


about active use of classes

The parsing phase of the class:

In the parsing phase, the Java virtual Opportunity replaces the symbolic reference in the binary of the class with a direct reference.

Initialization phase of a class

When a Java Virtual machine Initializes a class, all of its parent classes are required to be initialized, but this rule does not apply to interfaces

Initializing a class does not initialize the interface it implements

When an interface is initialized, its parent interface is not initialized first

The child class is actively used to cause the parent class to initialize

Active use of the parent class does not cause subclasses to be initialized

The active use of a class or interface can be considered only if the static variable or static method accessed by the program is actually defined in the current class or the current interface.

calling the LoadClass method of the ClassLoader class to load a class is not an active use of the class and does not cause the initialization of the class


The above points, only in the program can be truly manifested.

<pre name= "code" class= "Java" >package Com.tgb.classloader;public class Testsix {    static {        System.out.println ("Test static Block");    }    public static void Main (string[] args) {        System.out.println (child3.a);        Child3.dosomethind ();    }} Class Parent3 {    static int a = 3;    static {        System.out.println ("Parent3 static Block");    }    static void Dosomethind () {        System.out.println ("do Something");}    } Class Child3 extends Parent3 {    static {        System.out.println ("Child3 static Block");}    }

Test static block
PARENT3 static block
3
Do something

The above is the output order, Perhaps the biggest doubt is in child3.a this code, when called a static variable, you may think that the active use of child3, in fact, is wrong, only if the static class or variable exists child3, it can be active use, but static variable a exists in the parent class, so is the Paren The active use of T. It is not surprising that the output PARENT3 static block.

some of the underlying concepts may be less noticeable when learning, but if there are some phenomena that we cannot explain in the course of use, look back at the basic concept and the problem is


Java Re-learning-JVM class loading and execution 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.