Deep Java Virtual machine learning-the loading mechanism of classes (cont.)

Source: Internet
Author: User

Last night written in-depth Java Virtual machine learning-the loading mechanism of the class to 1:30, because the next day to work, did not write the demo in the previous article, today take time to fill in the example of last night to explain.

Here I first posted yesterday's two copies of the code, look again:

classsingleton{Private StaticSingleton Singleton =NewSingleton ();//location of the first piece of code     Public Static intCounter1;  Public Static intCounter2=0; Private StaticSingleton Singleton =NewSingleton ();//location of the second piece of code    PrivateSingleton () {Counter1++; Counter2++; }     Public StaticSingleton getinstance () {returnSingleton; }} Public classdemo{ Public Static voidMain (string[] args) {Singleton Singleton=singleton.getinstance (); System.out.println ("Counter1:" +singleton.counter1); System.out.println ("Counter2:" +singleton.counter2); }}

First code Execution Result:

Second code execution Result:

How the class is loaded

Let's review the loading order of the previous article

We know that a Java virtual machine allocates memory for a static variable of a class and sets the default initial value for the start of the preparation phase , where the initial value is usually the default 0 value of the type (for example, 0, 0L, NULL, FALSE, and so on). Instead of being explicitly assigned to a value in Java code. A lot of people still do not understand the default value of 0 and the display of the value of what exactly mean, below we distance:

 Public class sample{    privatestaticint a=1;     Private Static int b;}

The result of the above code after the preparation phase is:

A=0; b= 0;

You may have no doubt about b=0, and a=0; that is, "type default 0 value", that is, the preparation phase equals to the right of the 1 is not assigned to a, do not know this explanation can not understand, and the class initialization phase is the last step of the class loading process, to the initialization stage, To actually start executing the Java program code defined in the class. during the initialization phase, the Java virtual machine executes the initialization statement of the class, assigning the correct initial value to the static variable of the class.

private static int a=1; indicates that a is explicitly initialized to 1;
private static int B; Here b is not explicitly initialized, so at this point the value of B is still 0;
Case Analysis

Okay, so much to start with, we know that classes are executed from top to bottom, so the first code is parsed as follows

    1. When the Singletom class is in the preparation stage, because it just assigns the data type default value, so at this time counter1=0, counter2=0;
    2. When the Singletom class is in the initialization phase, 1 calls the instance of Singleton and the + + operation is performed for Counter1 and Counter2 respectively, so the counter1=1,counter2= at this time 1, because the counter1 is not explicitly initialized in 2, Counter1 still retains the value 1 at this time, and Counter2 is explicitly assigned to 0, so counter2 in the initialization phase is changed to 0
    3. The final result after the preparation and initialization phase becomes counter1:1, counter2:0

The second code is to change the order of the 1->2->3 to 2->3->1, and we re-analyze it according to the thought above and find it clear that we know the result.

There is no change in the preparation stage, counter1=0, counter2=0;

During the initialization phase, Counter1 is not explicitly assigned, so Counter1 still retains the value 0,counter2 is explicitly assigned a value of 0, so counter2=0, the singleton () method is called at the third step, and the + + operation is performed

Final results counter1:1, counter2:1

Deep Java Virtual machine learning-the loading mechanism of classes (cont.)

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.