Life cycle of the JAVA virtual machine

The mission of a runtime Java virtual machine instance is to run a Java program. When a Java program is launched, a virtual machine instance is created. When the program shuts down and exits, the virtual machine instance dies. If you run three Java programs simultaneously on the same machine, you get three Java virtual machine instances. Each Java program runs in its own Java virtual machine instance. A Java virtual machine instance runs a Java program by calling the main() method of an initial class. The main() method must be public, static, return void, and take an array of strings. Any class with such a main() method can be used as a starting point for Java programs to run.

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Hello World"); }}Copy the code

In the example above, the main() method in the Java program’s initial class will be the starting point for the program’s initial thread, which will start any other threads. There are two types of threads inside a Java virtual machine: daemon threads and non-daemon threads. Daemon threads are typically used by the virtual machine itself, such as threads that perform garbage collection tasks. However, a Java program can also mark any thread it creates as a daemon thread. The initial thread in a Java program, the one that starts at main(), is a non-daemon thread. As long as any non-daemon threads are running, the Java program continues to run. When all non-daemons in the program terminate, the virtual machine instance automatically exits. The program itself can also exit by calling the Exit () method of the Runtime class or the System class, if the security manager allows it.

JAVA Virtual Machine architecture

The following diagram shows the structure of a JAVA virtual machine. Each JAVA virtual machine has a classloading subsystem that loads types (classes or interfaces) based on a given fully qualified name. Similarly, each Java virtual machine has an execution engine that executes instructions contained in the methods of the loaded class.








Some run-time data areas are shared by all threads in the program, and some are owned by only one thread. Each Java virtual machine instance has a method area and a heap shared by all threads in that virtual machine instance. When a virtual machine loads a class file, it parses type information from the binary data contained in the class file. This type information is then placed in the method area. When the program runs, the virtual machine puts all objects created by the program at run time into the heap.
























The data type

Java virtual machines perform calculations using data types that can be divided into two types: primitive and reference types, where primitive variables hold primitive values and reference variables hold referenced values.








The Java virtual machine also has a basic type that is only used internally: returnAddress. Java programmers cannot use this type, and this basic type is used to implement the finally clause in Java programs. This type is used by JSR, RET, and JSR_W directives, and its value is a pointer to the OPcode of JVM directives. The returnAddress type is not simply a numeric value, does not belong to any of the basic types, and its value cannot be changed by a running program. Java VIRTUAL machine reference types are collectively referred to as “references.” There are three types of reference types: class type, interface type, and array type. Their values are all references to dynamically created objects. A value of a class type is a reference to a class instance; The value of an array type is a reference to an array object, which in the Java virtual machine is a real object; A value of an interface type, on the other hand, is a reference to an instance of a class that implements that interface. A special reference value is NULL, which indicates that the reference variable does not refer to any object.

Reference passing of method parameters in JAVA

There are two kinds of parameter passing in Java: value passing and reference passing. Passing by value needless to say, passing by reference. “When an object is passed as an argument to a method,” this is called passing by reference.

public class User {
    
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) { this.name = name; }}Copy the code
public class Test {
    
    public void set(User user){
        user.setName("hello world");
    }
    
    public static void main(String[] args) {
        
        Test test= new Test(); User user = new User(); test.set(user); System.out.println(user.getName()); }}Copy the code

The output of the above code is “Hello world”, which goes without saying, but what if we changed the set method to look like this?

  public void set(User user){
        user.setName("hello world");
        user = new User();
        user.setName("change");
    }
Copy the code

The answer is still “Hello world”, so let’s examine the code above. First of all,

User user = new User();
Copy the code

An object is created in the heap and a reference to the object is created in the stack, as shown below:





User user = new User();
Copy the code

The reference user is passed as an argument to the set method. Note that you are not passing the reference itself, but a copy of the reference. That is, there are two references (a reference and a copy of a reference) to objects in the heap, as shown below:





user.setName("hello world");
Copy the code

In the set() method, “copy of user reference” operates on the user object in the heap, setting the string “Hello world” to the name attribute. The diagram below:




user = new User();
Copy the code

In the set() method, we create another User object and point the “copy of User reference” to the newly created object in the heap, as shown below:




user.setName("change");
Copy the code

In the set() method, “copy of user reference” operates on the newly created User object in the heap.





After the set() method is executed, look back to mian()

System.out.println(user.getName());
Copy the code

Because “copy of user reference” had previously set the name property of the user object in the heap to “Hello world”, when getName() is called by user in the main() method,” Hello World “is printed. The diagram below:




Welcome to study and exchange group 569772982, let’s study and exchange together.