Home  >  Article  >  Java  >  What is main in Java

What is main in Java

(*-*)浩
(*-*)浩Original
2019-11-14 14:25:553630browse

What is main in Java

#The main method is a special method, which is the entry point for program execution. A Java program starts executing from the main method.

The Java virtual machine will allocate a main thread in advance before executing the Java program, and then execute our main method in the main thread. Do you understand?

So we often say that the main method is called the main thread! ​ ​ (Recommended learning: java course)

Threads are different execution paths in a program! Forget it, it doesn’t matter if you don’t understand these. In short, just remember that the Java program starts executing from the main method.

For example:

public static void main(String[] args) {
    System.out.println("nihao");
}

When your program is running, come in from the main method, and the first thing you do is

System.out.println("nihao");

Then there are no other statements, the main method ends, and the program ends!

The declaration of this main() method is: public static void main(String args[]). It must be defined this way, this is the Java specification.

Why it is defined in this way has something to do with the operation of the JVM.

When a class has a main() method, executing the command "java class name" will start the virtual machine to execute the main method in the class.

Since the JVM will first call the main method when running this Java application, it does not instantiate the object of this class when calling, but calls it directly through the class name, so it needs to be limited to public static. (Class name.main())

For the main method in java, jvm has restrictions and cannot have a return value, so the return value type is void.

There is also an input parameter in the main method, the type is String[], this is also the specification of Java, there must be an input parameter in the main() method, the type must be String[], as for the name of the string array , this can be set by yourself. According to custom, the name of this string array is generally consistent with the main parameter name in the sun java standard example, and is named args.

Therefore, the main() method definition must be: "public static void main(String string array parameter name [])".

The above is the detailed content of What is main in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn