2

Possible Duplicate:
Where and why JVM checks that the return type of entry method main(String args[]) is void and not anything else?
return type of main in java

After consulting these two questions What's the meaning of the return value of int main(), and how to display it? & What should main() return in C and C++?, I come up with another question:

Why Java program's main method should return void, but C/C++ program's main function should return int?

Community
  • 1
  • 1
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
  • 1
    Simply put, a Java program does not end because the `main` method (running on the main thread) ends. It ends **when all threads end**. Therefore there is no meaning to the return value of `main`. – Marko Topolnik Nov 10 '12 at 13:44

1 Answers1

2

Java specification:

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

And further:

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.

As you can see main return type might be meanless due to the exit of other threads.

Community
  • 1
  • 1
Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60