Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

main function does not return anything. Why? [duplicate]

Tags:

java

c++

c

jvm

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

like image 559
gameover Avatar asked Jan 15 '10 10:01

gameover


People also ask

Can return type of main function be double?

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.

Why main method does not have return type java?

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.

Does a main function need a return statement?

In a main function, the return statement and expression are optional.

Does main need a return statement C++?

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.


1 Answers

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.

like image 179
Kai Huppmann Avatar answered Sep 22 '22 20:09

Kai Huppmann