The virtual machine is located between the machine and the compiler, which is an abstract virtual machine. A virtual machine provides a common interface to the compiler on any platform, the compiler targets the virtual machine, generates code that the virtual machine can understand, and the interpreter translates the virtual machine code (in Java, the bytecode, class file, virtual machine only) to the machine code execution of the particular system. The interpreter for each platform is not the same, but the virtual machines that are implemented are the same. It can be said that the interpreter is a part of the virtual machine, a machine, as long as equipped with an interpreter, you can run the class byte code, regardless of what platform this class bytecode is compiled generated. The Java source program is compiled by the compiler into bytecode, the bytecode is interpreted by the virtual machine, the virtual machine sends each byte code to be executed to the interpreter, the interpreter translates it into machine code on a particular machine, and then runs on a particular machine.
Compiler interpreter
Java source file-------> Byte code (class file)---------> machine code
The JVM (Java Virtual machine) runs the process:
There are now two Java source files:
PackageDEMO.JVM; Public classUser {PrivateString name; PrivateString pwd; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getpwd () {returnpwd; } Public voidsetpwd (String pwd) { This. PWD =pwd; }}
PackageDEMO.JVM; Public classMain {Private Static intSize=1; Public Static voidMain (String args[]) {User u=NewUser (); U.setname ("Tom"); U.setpwd ("123"); String name=U.getname (); String pwd=u.getpwd (); U=NULL; } }
First compile the two source files into a class bytecode, then the JVM will execute:
1, load
The Java Virtual machine uses the class loader to navigate to the appropriate class file and then reads the Class file (a linear binary data stream) and passes it into the Java virtual machine. Immediately after the virtual machine extracts the type information. For example: Class name, method name, variable name, modifier, method return type, and so on. Another important thing is the constant pool. (a constant pool holds all constants of that type, including direct constants and symbolic references to other types, fields, methods), to store this information in a place called the method area. Finally , an instance of class is formed, which is stored in the heap area of memory. It becomes the interface between the Java program and the internal data structure, and the program accesses that type of information, and the program calls the method of the class instance object of that type. In short: This process is the process of parsing a type of binary data into an internal data structure within a method area and building a CLASS object on the heap. For example, loadMain class:java virtual machine read main class style= "Font-family:times New Roman;" >class files, producing corresponding java.lang.class class that reads the type information, such as modifiers private,static , the other variable size,name,pwd,user (
2, link
is divided into three steps: calibration, preparation, parsing.
1) Checksum: Determines the type conforms to java language Semantics, For example: final class cannot have subclasses, 2) Prepare: TheJava Virtual machine allocates memory for class variables and sets default values. 3) Parsing: The process of looking for classes, interfaces, fields, and methods that conform to references in a constant pool of types to replace these symbolic references with direct references. For example, a linkMain class:The Java Virtual machine allocates memory for size and assigns a default value of 0. Locate a reference to the user class in the constant pool, load and connect the class if the user class is not yet loaded, and then replace the reference to the user class with a direct reference in the constant pool. At this point the user class is not initialized because it is not yet used.
3, Initialize
Initialize some of the static variables
Procedure: The () method may be called ( this method can only be called by a Java Virtual machine) to initialize the static variables of the class. Before calling this method, you must confirm that the superclass () method of the class has been called. For example, initialize the main class:the Java Virtual machine assigns a static variable of the main class to a value of 1.
4, using (executing this type of code)
1.User u = new User (); (Heap area stored in memory)
A user class instance is created, and is actually instantiated through the class instance of the classes . Here's how:
User u= (user) Class.forName ("User"). newinstance ();
For convenience, use c instead of class.forname ("User")
2.u.setname ("Tom"); U.setpwd ("123");
Call the method of the class, assign a value to the variable of the class, theJava Virtual machine internal call is like this, through the method area to find the method, using the class instance of the following method call:
C.getmethod ("SetName"). Invoke (U, "Tom");
3.String name = U.getname (); String pwd = U.getpwd ();
Similar to the second step, the difference is that the obtained values are assigned to the variable name and pwd respectively. The key is where is this value stored? As with instance objects, it is stored in the heap area. This time I should be able to see the role of class instances, it is a central role in the program calls to reflect the data on the heap area changes.
4.U = null;
This step is written to look at the Java Virtual machine garbage collection mechanism. the Java Virtual machine internally determines whether the two classes can be recycled based on a rule (whether the object can be touched). The specific form is as follows:
When you execute u = NULL, the line is cut off, so the user instance cannot be touched, so the Java Virtual machine can reclaim the user instance.
Original: http://www.cnblogs.com/o-andy-o/archive/2012/04/11/2442109.html
How Java Virtual machines work