How Java Works
The concept of virtual machines is introduced in Java, which is to add a layer of abstract virtual machines between the machine and the compiler program. This virtual machine provides a common interface to the compiler on any platform. The compiler only needs to target the virtual machine, generate code that the virtual machine can understand, and then be executed by the interpreter to convert the virtual machine code to the machine code of the particular system. In Java, this code for the virtual machine is called bytecode (bytecode) (The contents of the class file), and it is not targeted at any particular processor, but only for virtual machines. The interpreter for each platform is different, but the virtual machines that are implemented are the same. Java The 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.
bytecode : Code usually refers to a program or part of a program that is readable to a person. Whereas bytes are units of storage size, computer-readable information is usually organized into bytes. The random term bytecode is a readable program for computers.
JAVA is a kind of language which is designed to be a semi-compiled and semi-explanatory language, while drawing on the characteristics of compiled and interpreted languages. It is also a complete set of IT industry solutions, including many technologies. As a whole, Java technology can be divided into Java programming language and Java platform.
Java programming language Features: Simple, object-oriented, portable, multi-threading, robust and secure, dynamic, structurally neutral, high performance, distributed.
Cross-platform:
In other words, in Beijing, generally speaking Beijing dialect, Shanghai, generally will be Shanghai dialect, Guangdong, Guangdong dialect ...
An official document issued, to the national implementation, should be how? --first unified translation into Mandarin. Translation of the Putonghua version into local dialects is available everywhere.
Here, Beijing, Shanghai are different types of machine windows,linux ...
Compilation (JAVAC) is the process of translating official documents into Putonghua, and the compiled. class file is the Putonghua version of the official document.
At the time of execution, the local translator is the JVM responsible for translating the. Class into a dialect that can be understood in a cost-effective manner.
The so-called platform refers to the program running hardware or software environment. Today's popular platform has Linux,windows,unix,macos and so on. The biggest difference between the Java platform and these platforms is that the Java platform is a pure software platform that runs on other hardware-based platforms. The Java platform has two components: a Java Virtual machine, a Java application programming interface.
Java Virtual machine (JVM)
Java Virtual Machine (JVM) is the abbreviation of Java VM, it is a fictitious computer, is realized by simulating various computer function simulation on the actual computer.
In Java, it takes three steps for the ClassLoader to load a class into a Java Virtual machine: load , link , initialize , link to verify, prepare, parse
Loading: Finding and importing. class files
Link: Check the correctness of the loaded. class file, then, the Java Virtual machine allocates memory for the variable, sets the default value
Initialize: Change the symbolic reference to a direct reference ...
Now that the two Java source files have been compiled into class files, let's take a look at how the Java virtual machine performs.
Java Virtual machine Workflow :
1. Loading
Describe: Java Virtual machine mounts the specified class file
Result: The instance object of the class class is formed
Procedure: 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 .
Example: Loading the main class
The Java virtual machine reads the class file of the main class, produces an instance of the corresponding Java.lang.Class class, reads the type information in it, such as the modifier private,public,static, the other variable size,name,pwd, User (user is a reference) together constitutes a constant pool of this class. Save this information in the method area,
2. Links
Description: Validate, prepare, parse (optional)
Result: This type is correct. (I don't know how to describe it here.)
Process:
1) Verify that the type conforms to the semantics of the Java language, such as: The final class cannot have subclasses, the final method cannot be overwritten, ensure that there is no incompatible method declaration between the type and the superclass (for example, two methods have the same name, the parameters are identical, but the return type is different).
2) Prepare: Java Virtual machine allocates memory for class variables, 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.
Example: Connecting the main 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. Initialization
Description: Initializes a number of static variables
Result: This type can be used
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.
Example: Initializing the main class
The Java virtual machine assigns a static variable of the main class to a value of 1.
4. Use (Execution of the 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 ("Li Wenshui"); U.setpwd ("159");
Call the method of the class, assign a value to the variable of the class, the Java 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, "Li Wenshui");
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. (No practical significance)
Does the Java virtual machine internally determine whether the two classes can be recycled based on a rule (whether this 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.
The above is the operation of Java, the shortcomings, welcome to communicate.
Execution of Java