Good programmers to learn Java JVM route sharing related concepts

     Good programmers to learn Java JVM route sharing related concepts, jdk (Java Development Kit) Java Development Kit is a set of Java developers a set of procedures for compiling and debugging of programs.


    jre (Java Runtime Evironment) Java Runtime Environment, is a platform for running Java programs, all Java programs can only be executed in this platform.

    jvm (Java Virtual Machine) Java Virtual Machine, is out of the virtual computer codes used to simulate the functions performed by a computer, it has its own hardware architecture, such as: processor, stack, registers, etc., also has its own set of instructions systems, on different operating systems can be installed JVM, enabling Java programs can execute on different operating systems, JVM is to achieve Java's cross-platform features.

 JVM class loading process

We developed a Java program execution, you need to compile and then executed, JVM is responsible for the process of loading the class. <br>

Class loading process is divided into:

1. Load

2. Verify

3. Prepare

4. resolve

5. Initialize

DETAILED class loading procedure

The following details under these procedures:

1. Load <br>

    In the process of loading the class to complete:

    1. In accordance with the full name of the qualifier class, obtain class binary stream, the stream can, jar files obtained from the class on the disk, can also be obtained from the network.

    2. The dynamic storage structure static storage class structure of the region into an operating method

    3. In the heap memory to generate the corresponding java.lang.Class objects, a method as an inlet region

2. Verify <br>

    After loading the class is completed, enter the verification process, which ensures that the information generated in the previous Class object will not endanger the safety of the JVM. <br>

    Need to verify aspects are:

    1. The file format validation is to verify compliance with specifications Class byte stream file format, and can be the current version of virtual machine processing. The magic number, to verify whether 0xCAFEBABE; the major and minor version number is the current virtual machine processing range; if there is not constant type supported constant in the constant pool, etc., the main purpose is to ensure the verification phase input byte flow properly parse and stored in the process region, are authenticated at this stage, the byte stream will enter the storage method of the memory area, so that the latter three are based verification phase memory area configuration method performed.

    2. metadata verification information is described bytecode semantic analysis, to ensure compliance with the information which describes the Java language specification. The validation may include: whether a parent class; whether the parent of this class inherit the inherited class allowed; if the class is not an abstract class, whether implements all the methods of its parent class or interface implementation requirements.

    3. bytecode verification, the main task is the data flow and control flow analysis to ensure the method validation class at runtime will not make a virtual machine acts endangering safety. If the byte code method body does not pass a class bytecode verification, it is certainly a problem; but if a method body by the byte code verifier can not explain it necessarily safe.

    4. Verify reference symbol, the symbol occurs in the virtual machine, when converted to a direct reference cited, the conversion action will occur in "Analytical phase". Symbolic reference classes, fields and methods; validation symbol strings described by reference to the naming authority if a corresponding class can be found; whether there is a method in compliance with a method specified field descriptor class name and a simple description of the fields and accessibility (private, protected, public, default) whether the current class can be accessed.

3. Prepare <br>

    Preparation phase will be allocated in the method area for static variables like memory and assign default values.

    ```

    public static int count = 100;

    ```

    Such as: the above count variable in the preparation phase will be assigned to 0 and then assigned to 100 during initialization;

4. resolve <br>

    Parsing stage is a virtual machine to a constant pool of symbolic references to the process of replacing direct references.

    - symbolic references (Symbolic Reference) <br>

        Symbol reference to a certain set of symbols described in the referenced, literal symbols may be in any form, can be positioned unambiguously as long as the goal to use. Symbol reference has nothing to do with the memory layout to achieve a virtual machine, the goal is not necessarily a reference has been loaded into memory.

    - a direct reference (Direct Reference) <br>

        A direct reference may be a direct pointer to the target, a relative offset or indirectly targeted to handle targets. Direct reference to the memory layout is related to implementation of the virtual machine, if you have a direct reference, the target reference must already exist in memory.

5. Initialize <br>

    Class initialization class is the final step of the loading process, the front loading type, by addition to defining class loader from participation, the remaining operation is completely dominated by the virtual machine control during the load phase and the user application. To the initialization phase, really started classes defined in Java code. <br>

    Initialization phase is performed during the class constructor <clinit> () method. <Clinit> () method is collected automatically by the compiler class assignment operation of all classes and static variables of the block of statements (static {} block) merge statements generated.


    So when to perform initialization it?

    1. Create an instance of the class

    2. Access class static variable (except the constant, final modified)

        The reason: a special kind of constant variables, because the compiler treats them as property values ​​rather than to treat.

    3. Access class static method

    4. The reflector (Class.forName ( "com.test.Person"))

    5. When initializing a class, find its parent has not been initiated, the first call the parent class initialization

    6. When the virtual machine starts, the definition of the main () method to initialize the class

Code Case ####

Understand the class loading mechanism, we look at a face questions:

```

public class MySingleton {


    private static MySingleton singleton = new MySingleton();

    public static int count1 = 0;

    public static int count2;

    

    private MySingleton () {

        count1++;

        count2++;

    }

    

    public static MySingleton getInstance(){

        return singleton;

    }

    

    public static void main(String[] args) {

        MySingleton Singleton MySingleton.getInstance = ();

        System.out.println("count1-->"+MySingleton.count1);

        System.out.println("count2-->"+MySingleton.count2);

    }

}

```


The above results, the majority of students may think that two static variables are 1, the result of more accidents:

```

count1-->0

count2-->1

```

Why is this? Now we have to analyze:

1. First of all we know will assign default values ​​for static variables in the preparation phase class: <br>

singleton = null;

count1 = 0;

count2 = 0;

2. When the static method call the class getInstance, triggered initialization class, perform new MySingleton () constructor call, then: <br>

count1 = 1;

count2 = 1;

3. Continue initialization for variable assignment, count1 assigned 0, count2 no assignment to retain the value 1, the result is: <br>

count1 = 0;

count2 = 1;

#### to sum up

JVM is the code of the computer simulation, with its own hardware and software, JVM enables Java classes to load and run specific loading process are: loading, validation, preparation, analytical, initializing five steps.


Guess you like

Origin blog.51cto.com/14479068/2429541