Java Execution Process

Source: Internet
Author: User

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), which 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. 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.

Java Virtual machine (JVM)

Java Virtual Machine (JVM) is the abbreviation of Java Virtualmachine, it is a fictitious computer, it 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

Connect : Check the correctness of the loaded. class file, and then the Java Virtual Machine allocates memory for the variable, setting the default value

Initialize : The symbolic reference becomes a direct reference.

  1. public class Main {
  2. private static int size=1;
  3. public static void Main (String args[]) {
  4. User U = new user ();
  5. U.setname ("Li Wenshui");
  6. U.setpwd ("159");
  7. String name = U.getname ();
  8. String pwd = U.getpwd ();
  9. U = NULL;
  10. }
  11. }
  12. public class User {
  13. private String name;
  14. Private String pwd;
  15. Public String GetName () {
  16. return name;
  17. }
  18. public void SetName (String name) {
  19. THIS.name = name;
  20. }
  21. Public String getpwd () {
  22. return pwd;
  23. }
  24. public void SetPwd (String pwd) {
  25. This.pwd = pwd;
  26. }
  27. }

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: The Java Virtual machine loads 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, such as the modifier private,public,static, and the variable Size,name,pwd,user (User is a reference) together to form a constant pool of this class. Save this information in the method area,

2. Connect

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)

When the U =null is executed, the line is cut off, so the user instance cannot be touched, so the Java virtual machine can reclaim the user instance.

Java Execution Process

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.