Detailed explanation of Java main method

1. Starting with the main method

After compiling our java file, we need a class with a main method, the java command will instruct the operating system to start a jvm process

After the jvm process starts, look for the main place to start executing the program

java [JVM_Options] ClassName_with_main [args_separate_space]

The signature of the main method must be public static void main(String[] args) why?

Simpler:

First, the main method is automatically called by the JVM (java virtual machine)

The location where the JVM calls the main method is naturally not in a certain class or a certain package, so it is only visible to the JVM when the main method is at the public level, so the mian method needs public modification,

The class where the main method is located also needs the public modifier.

Since the main method is the entry point of all programs, that is, no object is created when main is called, and a method is not called through an object, only the method is defined as a static method, so the main method is a static method, which requires static modification.

The JVM is already the bottom layer for a java program, and the return value of the method called by it has nowhere to go. Therefore, the return value of the main method is empty, which needs to be modified with void.

As for the parameter String[ ] arg of the main method, we have few opportunities to use it now, it is used to accept the parameters passed in on the command line

 2. What happened before the main method was executed

You can refer to  jvm source code analysis     

First of all, it must be clear that the jvm process is a process of the operating system, and the process is a multi-threaded mechanism.

We define two kinds of threads:

JVM thread : refers to the thread managed by the JVM itself, we cannot control it in the program, and most of it is a guardian type

Java thread : refers to the thread generated by JVM from the perspective of java technology, we use the Thread class or Runnable interface to write in the program, the controllable thread

As for how java threads are implemented in jvm and how they correspond to os-level threads, please see   http://my.oschina.net/jingxing05/blog/275334

After specifying two different types of threads, before executing the main method: LoadJavaVM

The jvm process starts multiple jvm threads ( probably wrong, if so, please enlighten me ):

jvm thread:

  • Start VM Thread, the singleton, the ancestor of all threads! This thread fetches tasks from a queue from the polling loop to spawn other threads

  • According to the jvm abstraction specification, there may be execution engine threads, GC threads, classloader threads

After the jvm itself starts and initializes, it will

ContinueInNewThread(JavaMain, threadStackSize, (void*)&args);

That is to start a thread called main to execute the main method of the entry. Although the main thread is not a thread we manually generated, it is still a non-daemon thread.

3. Main execution process

  • load class

When the main method is executed, the jvm process finds that the class where main is located is not in the method area, so it starts classloading

The last step after the class is loaded is to decide whether   to initialize the class according to the situation

The class must be initialized before main is executed. Initialize class variables, and static code blocks. When initializing, initialize its parent class first. Every class has an implicit parent class Object. 

The order of initialization: class variables and static blocks are in order, parent first, then child

When the initialization process of the class occurs: 

1. T is a class, when an instance of T is created, that is, T t = new T(); 

2. When a static method of T is called, that is, T.staticField(); 

3. When the static property of T is assigned, T.staticField = o; 

4. When a static property of T is used, that is, Object o = T.staticField; but it is not a constant. 

5. T is a top level class , and an assert statement  lexically nested 

within T is executed. (do not understand, solve) 

  • execute the main method

The parameters, local variables, local methods, operands, etc. required by the method are pushed to the stack area of ​​the main thread in the structure of the stack frame, and then the execution engine thread starts to execute. After the execution, the stack frame is popped off. When there is no stack frame in the stack area of ​​the main thread, the main thread disappears.

  • unload class object

This step is an optimization step. It releases some memory in the method area. The jvm decides whether to use this step or not. Generally, it will not unload the method area.

  • program exit

1. All non-daemon threads are terminated 

2. A thread called the exit method of the class Runtime or System

main program execution diagram


Class loading detailed process


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326015966&siteId=291194637