66

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?

Mark
  • 1,459
  • 21
  • 20

4 Answers4

72

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):

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

Ankur Singhal
  • 23,626
  • 10
  • 70
  • 108
Zach Scrivena
  • 27,585
  • 10
  • 60
  • 72
28

This is an interesting discussion on velocityreviews on the same topic:

Highlight:

Incidentally, this is considered bad style in C and C++ just because it's the wrong signature for main, not for any universal reason independent of programming languages. It's one of those things that is not really supposed to work, but might on your implementation.

In Java, the reason main returns void is threads. C and C++ were both designed as languages before multithreading was a widely known technique, and both had threads grafted onto them at a later date. Java was designed from the beginning to be a multithreaded environment, and frankly, it would be unusual to write any non-trivial Java application that doesn't use more than one thread. So the idea that a program moves linearly from the beginning to the end of main is a bit outdated.

written by

www.designacourse.com The Easiest Way to Train Anyone... Anywhere. Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation

Anirudha
  • 30,881
  • 7
  • 64
  • 81
Nicolai
  • 3,328
  • 3
  • 28
  • 34
24

The reason for the main method having void as return type is that once main finishes, it doesn't necessarily mean that the entire program finished. If main spawns new threads, then these threads can keep program running. The return type of main doesn't make much sense at this point.

For example, this is very common in Swing applications, where the main method typically starts a GUI on the Swing thread, and then main finishes... but the program is still running.

Nayuki
  • 16,655
  • 5
  • 47
  • 75
Peter Štibraný
  • 31,128
  • 15
  • 85
  • 114
  • Peter, could you clarify for myself (currently exploring java multithreading)... Is it deamon threads ONLY that are NOT permitted to outlive main() ? – Max Jul 16 '10 at 06:11
  • 5
    @Max: no, main() method has no control over JVM. When JVM starts, it will run main() method, but when main() finishes, it doesn't mean that JVM terminaes. JVM continues to execute all threads until 1) Runtime.exit() is called OR 2) all normal (not daemon) threads have died. Daemon threads do not count for this second condition. In other words ... if main() method spawns some normal threads, JVM will **not** terminate when main() finishes. If main() doesn't spawn any threads, JVM will terminate. If main() spawns only daemon threads, JVM will also terminate when main() finishes. – Peter Štibraný Jul 16 '10 at 06:44
  • This answer makes the most sense to me out of all the answers. – Aaron Franke Jan 24 '17 at 18:20
7

You can return an int with System.exit().

Returning anything other than an integer doesn't make much sense, as the OS expects an integer. In case nothing is returned the default is 0, which means OK. Other values typically are used to signal errors or special conditions.

starblue
  • 51,675
  • 14
  • 88
  • 146