InJavaIntroduces the concept of virtual machines, that isProgramAn abstract virtual machine is added between them. This virtual machine provides a common interface for compiling programs on any platform. To compile a program, you only need to target the virtual machine to generate a virtual machine that can understand it.CodeThe interpreter then converts the virtual machine code to the machine code of a specific system for execution. InJavaThe code for virtual machines to understand is calledBytecode(Bytecode) (class file content)It is not oriented to any specific processor, but to virtual machines. The interpreters on each platform are different, but the virtual machines implemented are the same.JavaAfter the source program is compiled by the compiler, it becomes a bytecode, which is interpreted and executed by the virtual machine. The virtual machine sends each bytecode to the interpreter, the interpreter translates it into machine code on a specific machine and runs it on a specific machine.
Cross-platform:
In other words, Beijing generally talks about Beijing. Shanghai generally uses Shanghai, Guangdong, and Cantonese...
What should I do if a document is issued nationwide? -- First, it is translated into Mandarin in a unified manner. Mandarin versions are translated into local dialects.
Here, Beijing and Shanghai are different types of machines windows, Linux...
Javac is the process of translating documents into Mandarin, And the compiled. Class file is the mandarin version of the document.
During execution, the Local translation is JVM, which is responsible for executing the dialect that can be understood at the cost of converting. Class.
|
*. JavA →*. Class→ Machine code
JavaCompiler(Compile)→ Virtual Machine(Explain execution)→ Interpreter(Translation)→ Machine code |
JavaVirtual Machine(JVM)
JavaVirtual Machine(JVM)YesJava Virtual MachineAbbreviation,It is a fictitious computer.,It is achieved through simulation of various computer functions on the actual computer.
Java, The Class Loader loads a classJavaThe virtual machine requires three steps:Load,Link,Initialization, WhereLinks are used for verification, preparation, and resolution.
Load: Search and import. ClassFile
Link: check mounting. ClassFile correctness, and then,JavaThe VM allocates memory for the variable and sets the default value.
Initialization: converts a symbolic reference to a direct reference...
View code
1 Public Class Main { 2 3 Private Static Int Size = 1 ; 4 5 Public Static Void Main (string ARGs []) { 6 7 User u = New User (); 8 9 U. setname ("Li Wenshui" ); 10 11 U. setpwd ("159" ); 12 13 String name = U. getname (); 14 15 String Pwd = U. getpwd (); 16 17 U = Null ; 18 19 } 20 21 } 22 23 Public Class User { 24 25 Private String name; 26 27 Private String PWD; 28 29 Public String getname (){ 30 31 Return Name; 32 33 } 34 35 Public Void Setname (string name ){ 36 37 This . Name = Name; 38 39 } 40 41 Public String getpwd (){ 42 43 Return PWD; 44 45 } 46 47 Public Void Setpwd (string PWD ){ 48 49 This . Pwd = PWD; 50 51 } 52 53 }
Now assume that the twoJavaThe source file has been compiledClassFile. Let's take a look.JavaHow to run the VM.
JavaVirtual Machine workflow:
1.Load
Description:JavaThe VM loads the specifiedClassFile
Result:ClassClass Instance Object
Process:JavaThe VM uses the class loader to locate the correspondingClassFile, and then readClassFile (A linear binary data stream).JavaVirtual Machine. Then the virtual machine extracts the type information. For example, the class name, method name, variable name, modifier, and return type of the method. Another important thing is the constant pool. (The constant pool stores all constants of this type, including direct constants and symbolic references to other types, fields, and methods.) Save the information inMethod Area. Final FormationClassClass instance, which is stored in the heap area of the memory. It becomesJavaThe interface between the program and the internal data structure. To access the information of this type, the program calls the correspondingClassMethod of the Instance Object. In short:In this process, a type of binary data is parsed into an internal data structure in the method area, andClassObject process.
Example: LoadMainClass
JavaVirtual Machine readMainClassClassFile to produce the correspondingJava. Lang. ClassClass to read the type information, such as ModifierPrivate,Public,StaticAnd other variablesSize,Name,PWD,User(UserIs a reference) to form a constant pool of this class. Save the information in the method area,
2.Link
Description: verification, preparation, and resolution (optional)
Result: The type is correct. (I don't know how to describe it here)
Process:
1)Verify: confirm that the type meetsJavaLanguage semantics, such:FinalClass cannot have subclass,FinalMethods cannot be overwritten to ensure that there is no incompatible method declaration between types and super types (for example, two methods have the same name, the parameters are identical, but the return types are different ).
2)Preparation:JavaVM allocates memory for class variables and sets the default value
3)Resolution: searches for classes, interfaces, fields, and methods in the constant pool of types to replace these symbol references with direct references.
Example: ConnectionMainClass
JavaThe Virtual Machine isSizeAllocate memory and assign default values0.Locate the constant poolUserClass reference, ifUserIf the class has not been loaded, load and connect the class, and thenUserReplace class references with direct references. At this timeUserClass is not initialized because it is not used yet.
3.Initialization
Description: initializes some static variables.
Result: this type can be used.
Process: may call()(This method can onlyJavaVirtual Machine call) to initialize the static variables of this class. Before calling this method, you must confirm that()The method has been called.
Example: initializationMainClass
JavaVirtual MachineMainClass static variables assigned1.
4.Use (this class of code is executed)
1. User u = new user ();(Stored in the heap area of the memory)
AUserThe class instance actually uses this classClassInstance instantiation. The method is as follows:
User u = (User) class. forname ("user"). newinstance ();
For convenience, useCReplaceClass. forname ("user ")
2. U. setname ("Li Wenshui"); U. setpwd (" 159 ");
Call the method of this class and assign values to the variables of this class,JavaThis is the internal call of the virtual machine. You can find this method in the method area and useClassThe following method is used to call an instance:
C. getmethod ("setname"). Invoke (u ,"Li Wenshui");
3. string name = U. getname ();String Pwd = U. getpwd ();
Similar to step 2, the difference is that the obtained values are assigned to the variables respectively.NameAndPWD. The key is where this value is stored? Same as the instance object, it is stored in the heap area. At this time, I can see thatClassThe role of an instance is to act as an intermediate function, reflecting the changes in data in the heap.
4. U = NULL;
The purpose of this step is to understandJavaVirtual Machine garbage collection mechanism. (No practical significance)
JavaThe Virtual Machine is subject to one rule.(Can this object be touched?)To determine whether the two classes can be recycled? The specific form is as follows:
When executedU = NULL;This line is broken, soUserThe instance cannot be reached, soJavaThe virtual machine can recycle thisUserInstance