With respect to C/C++ main() must always return an integer(zero to indicate success and non-zero to indicate failure). I can understand this as the program is run it becomes a process and every process should have an exit status, which we obtain by doing echo $? from shell after the process gets over.
Now I don't understand why is the main method does not return anything in Java? Has it got anything to do with the fact that the program is run on JVM and the JVM process is reposnsible for the returning of exit status?
Please clarify.
Thanks,
Roger
The function named main that acts as the entry point of a program in a hosted implementation of C must return an int. It's possible to write a function named 'main' that returns a double.
Java main method doesn't return anything, that's why it's return type is void . This has been done to keep things simple because once the main method is finished executing, java program terminates. So there is no point in returning anything, there is nothing that can be done for the returned object by JVM.
In a main function, the return statement and expression are optional.
without encountering a return statement, return; is executed. If control reaches the end of the main function, return 0; is executed. Flowing off the end of a value-returning function (except main) without a return statement is undefined behavior.
Designed when multi-threading was already a common thing, java said (by design) "good bye" to the idea that when 'main' returns the program is done. That's why there's no return value. As the others said use System.exit
when you want to exit with return code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With