The answer is no, the return type of the main() method must be void , and any other type is unacceptable.

Let's test and verify that the return type of the main() method is int, as follows:

 package com.magic.main;

public class MainDemo {

    public static int main(String[] args) {
        System.out.println("Hello World");
        return 1;
    }
    
}

(1) Use the javac MainDemo.java command to compile first, and you will get the MainDemo.class file;

(2) Run the java MainDemo.class command again, and you will see the following error output

 错误: 找不到或无法加载主类 MainDemo.class

As can be seen from the above, changing the return type of the main() method, although the compilation is successful, it cannot be run!

Note:

If you run the above MainDemo directly with the IDE tool, it will also fail to run. For example, if you use IntelliJ IDEA, the following error message will be output:

Error: The main method must return a null type value in class com.magic.main.MainDemo, please define the main method as:
public static void main(String[] args)

Since the return type of the main() method must be void, the question arises: Does the parameter required by the main() method in Java have to be an array of strings?

For more knowledge points related to Java interviews, you can pay attention to the [Java Interview Manual] applet, which involves Java foundation, multithreading, JVM, Spring, Spring Boot, Spring Cloud, Mybatis, Redis, database, data structure and algorithm.


十方
226 声望433 粉丝