Talk about Java virtual machines

Source: Internet
Author: User

This article can be used as a class note for Saint Sian in-depth Java virtual machine in Beijing.
First look at a Dan Teng face test.
public class  singleton{public static Singleton s=new Singleton ();    public static int k1;public static int k2=0;private  Singleton () {k1++;k2++;} public static void Main (string[] args) {System.out.println (SINGLETON.K1); System.out.println (SINGLETON.K2);}}


What is the output, please?

What if it's the following?

public class  singleton{public    static int k1;public static int k2=0;     public static Singleton s=new Singleton ();p rivate  Singleton () {k1++;k2++;} public static void Main (string[] args) {System.out.println (SINGLETON.K1); System.out.println (SINGLETON.K2);}}

Gentlemen, crossing, let's take a slow look at the problem from the virtual machine hierarchy.
What is a virtual machine? What is a virtual machine?
This information everyone Baidu, I will not repeat it?
The life cycle of a virtual machine in the following scenarios, the Java Virtual machine will end what period.
1 performed the System.exit () method
By looking at the API documentation, we exit with the parameter int, and when the argument is 0 it ends normally, otherwise it ends abnormally.
2 Normal end of program
3 Exception or error encountered during program execution
4 Operating system error occurred
The general diagram of the load, connection, and initialization of the class is as follows


Loading: Finding and loading binary data for a class
Connection:
-Verify that the class being loaded is correct
-Prepare to allocate memory for static variables of the class and initialize them to default values (int is 0 Boolean or false reference type is NULL)
-Parse To change a symbol reference in a class to a direct reference
Initialize: Assigns the correct initial value to the static variable of the class
I wonder if you have any questions about crossing?

If everyone doesn't, I have a problem, when we first wrote Java, always javac commands compiled into class bytecode, Java commands run. If there is something wrong with the Java code, the problem will be thrown in the javac, in other words, when we connect to the class file it must be OK, what else does it verify?
The answer is:
If the class is produced only through the Javac command, then there is no problem, but the key is that people can also manually generate class files, so it is useful to verify this step.
The second problem is that the static variable is initially initialized to the default value during the preparation phase of the connection, but the initialization stage also gives the correct initial value for the static variable of the class. What is the relationship between these two steps?
Look at the following code slice

public class Test{public static int a=5;public static int b;public static int c;static{    c=10;} public static person p;}

In the preparation phase, both A and B values are 0 (because the int type is 0 by default) p is null (person is a simple class)
To the initialization phase, A is 5, and B is still 0,c is 10,p or null.
In other words, the value of the assignment only works in the initialization phase. (The assignment to A is the same as the static code block below)
(The order in which the static variables are assigned in the preparation and initialization phases is in the order in which the variables are written.) The above example is to give a again to B, then C, the last p)

Step-by-step
Loading class loading refers to reading the binary data in the class's. class file into memory, placing it in the method area of the run-time data area, and then creating a Java.lang.Class object in the heap that encapsulates the data structure of the class within the method area.
Such as

The end product loaded by the class is a class object that is located in the heap area
There's another noun here. Class object.
Just like every book has author, price, etc. describe some of the familiar information, each class also has a description of their own information, it is the class object.
For example
public class person{
public String name;
Public String GetName () {
return name;
}
}
The class object contains a few attributes in the class, what type each property is, what method it is, what the parameters of each method are, what the return value is, and so on.
For specific information, refer to the API.
When it comes to class loading, you have to say the loader of the class.
There are two kinds of loaders in Java
–java the loader that comes with the virtual machine
Root class loader (Bootstrap)
(in C + + implementation is not visible to everyone)
Extension class loader (Extension)
System class loader (systems)
(Extension class loader and system ClassLoader are written in Java)
-user-defined class loader
Subclass of Java.lang.ClassLoader
Users can customize how classes are loaded
Look at the following code slice

                 Class c1=class.forname ("java.lang.String"); Class c2=class.forname ("Singleton");      That is the SingletonSystem.out.println (C1.getclassloader ()) above; System.out.println (C2.getclassloader ());
The result of the operation is
Null
[Email protected]
String is bootstrap loaded, written in C + + We do not see, so is null
Appclassloader is the system loader


After the validation class of the connection is loaded, it enters the connection phase. A connection is the merging of binary data from a class that has been read into memory into the runtime environment of the virtual machine.
Such as
The preparation of the connection has been explained above.
The connection is resolved as

As for what is a symbolic reference, everyone in Eclipse opens a class file


The red box above is the symbol reference

Initialization
The way Java programs use classes can be divided into two types
Active use
Passive use
All Java Virtual machine implementations must be in each class or interface by a Java program "first-time active use"When they are initialized.

Active use consists of the following six scenarios
Create an instance of the class (such as new Person () in code)
Access a static variable for a class or interface, or assign a value to the static variable (singlean.a=8)
Invokes the static method of the Class (Singleton.getinstance ();)
Reflection (e.g. Class.forName ("Com.shengsiyuan.Test")
Initializes a subclass of a class (with the Father class, which has a child class, and children inherits or implements the Father class. )
Class that is marked as the startup class when the Java Virtual machine starts (calls to Java startup commands such as Java Test)


In addition to the above six, other methods of invocation are passive use.


Say the first question.

public static Singleton s=new Singleton ();    public static int k1;public static int k2=0;private  Singleton () {k1++;k2++;}
K1,K2 is 0,s is null during the preparation phase of the connection

At the initialization stage, the initialization of S has called the constructor

K1,K2 is programmed from 0 to 1.

Continue Reading

    public static int k1;public static int k2=0;
K1 not been assigned or 1

But K2 was assigned a value of 0.

So the final result is

1

0

As for the

public static Singleton s=new Singleton ();

Put it in the back, let's analyze it for ourselves.




Talk about Java virtual machines

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.