Why is main() in java void?

java

Solution

The `main()` method must indeed have a `void` return type. From the Java Language Specification on "Execution - Virtual Machine Start-Up" (§12.1.4):

The method `main` must be declared `public`, `static`, and `void`. It must accept a single argument that is an array of strings.

It goes on to describe when a program exits in "Execution - Program Exit" (§12.8):

A program terminates all its activity and exits when one of two things happens:

- All the threads that are not daemon threads terminate.

- Some thread invokes the `exit` method of class `Runtime` or class `System` and the exit operation is not forbidden by the security manager.

In other words, the program may exit before or after the `main` method finishes; a return value from `main` would therefore be meaningless. If you want the program to return a status code, call one of the following methods (note that all three methods never return normally):

- `System.exit(int status)` - Equivalent to `Runtime.getRuntime().exit(status)`

- `Runtime.exit(int status)` - Terminates the currently running JVM by initiating its shutdown sequence (run all registered shutdown hooks, and uninvoked finalizers, if necessary). Once this is done the JVM halts.

- `Runtime.halt(int status)` - Forcibly terminates the currently running JVM.

Of the three, `System.exit()` is the conventional and most convenient way to terminate the JVM.

Problem

In the case of languages with a C-like syntax, we declare the main() method to return an int or float value (or void). Is it possible to declare a non-void return type from main() in Java? If not, then why not? Does this mean that a Java program doesn't return any value to the OS?

Original source

Related problems