1. Execution process of Java program
Before understanding why a main method in Java can start a project, we need to understand the execution flow of a Java program. During the execution of a Java program, the JVM (Java Virtual Machine) first loads the Java bytecode file, then interprets the bytecode instructions, and executes the methods and statements in the Java program during the interpretation process.
The execution flow of the Java program is as follows:
- Java source files are compiled into bytecode files (.class files) by a compiler;
- JVM loads bytecode files, interprets bytecode instructions, and executes methods and statements in Java programs during the interpretation process;
- After the Java program is executed, the JVM returns the program execution result to the operating system.
2. Java program entry
The Java program entry refers to the starting point of the program, that is, the entry method of the program. In a Java program, the main method is the entry method of the program.
In a Java program, each class can contain a main method. The main method must be static and have a public modifier, its method name is main, and its parameters are a String array. When the JVM starts a Java application, it looks for a class containing the main method and executes that method.
3. The startup process of the Java program
The startup process of the Java program is as follows:
- JVM start;
- The JVM looks for the class containing the main method;
- The JVM loads the class containing the main method;
- The JVM executes the main method.
A Java application starts running when the JVM executes the main method. Therefore, the main method is the entry method of the Java program and the startup method of the Java application.