Java main() Method

Can the main method be int? If not, why?

No, Java main method can not be int. There are two main reasons for it:

  1. JVM looks for public static void main(String[] args) when starting the program execution as it is the standard signature for entry. Using int signature would cause confusion and compatability issues while program execution.
  2. Having void signature means the main method will not return anything, but using int signature means the main function will have to return integer, which isn’t useful for JVM.

Can we execute a Java program without the main method?

No, From JDK7 main method is mandatory. The compiler will verify first, whether main() is present or not. If your program doesn’t contain the main method, then you will get an error “main method not found in the class”.

To check more about the topic refer to Is main method compulsory in Java? article.

Can we declare the main() method without String[] args?

Yes, we can declare the main() method without String[] args. Although it will generate an error message if we directly try to execute the main method inside the driver class as done in the below example.

Below is the correct method to write a program without String args[].

Java
import java.io.*;
import javafx.application.Application;

abstract class GFG
    extends javafx.application.Application {

    // static block
    static
    {
        System.out.println("Hello, world!");
        System.exit(0);
    }
}

Output

Hello, world!

Why is main void in Java?

The main method serves as the starting point for program execution in Java. The JVM doesn’t expect a return value from the main because there’s no mechanism for it to receive or process such a value.



Java main() Method – public static void main(String[] args)

Java’s main() method is the starting point from where the JVM starts the execution of a Java program. JVM will not execute the code, if the program is missing the main method. Hence, it is one of the most important methods of Java, and having a proper understanding of it is very important.

The Java compiler or JVM looks for the main method when it starts executing a Java program. The signature of the main method needs to be in a specific way for the JVM to recognize that method as its entry point. If we change the signature of the method, the program compiles but does not execute.

The execution of the Java program, the java.exe is called. The Java.exe in turn makes Java Native Interface or JNI calls, and they load the JVM. The java.exe parses the command line, generates a new String array, and invokes the main() method. By default, the main thread is always a non-daemon thread.

Similar Reads

Syntax of main() Method

Syntax of the main() method is always written as:...

Overloading main() Method in Java

Overloading the main() method is possible in Java, meaning we can create any number of main() methods in a program....

Conclusion

Java main() method is the starting point of a Java program. It is the main body that is executed by the JVM, and without the main() method no Java program can be run without it....

Java main() Method- FAQs

Can the main method be int? If not, why?...