Why main () in java void?

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

+55
java
Feb 12 '09 at 7:50
source share
4 answers

The main() method must have a return type of void . From the Java Language Specification in the section “Execution - Starting a Virtual Machine” ( §12.1.4 ):

The main method must be declared public , static and void . This should take one argument, which is an array of strings.

The following describes when a program exits "Execution - Program Exit" ( §12.8 ):

The program stops all its activities and quits when one of two things happens:

  • All threads that are not daemon threads terminate.
  • Some topics call the exit method of the Runtime class or class System , and the exit operation is not prohibited by the security manager.

In other words, the program may exit before or after the main method completes; therefore, the return value from main would be pointless. 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 usual and most convenient way to complete the JVM.

+64
Feb 12 '09 at 8:28
source share

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

Highlight:

By the way, this is considered bad style in C and C ++ only because it is the wrong signature for the main, and not for any universal reason, regardless of programming languages. This is one of those things that really shouldn't work, but may be on your implementation.

In Java, the reason main returns void is threads. C and C ++ were both developed as languages ​​before multithreading was widely known by the technician, and both of them had vaccinations on them later. Java was designed from the very beginning to be a multi-threaded environment, and frankly, it would be unusual to write any non-trivial Java application that does not use more than one thread. So, the idea that the program moves linearly from beginning to end main is a bit outdated.

written

www.designacourse.com The easiest way to train anyone ... anywhere. Chris Smith - Lead Software Developer / Technical Trainer MindIQ Corporation

+27
Feb 12 '09 at 8:01
source share

The reason for the main method, which has void as the return type, is that after main completes, this does not necessarily mean that the entire program is complete. If main spawns new threads, then these threads can support the program. The return type main does not make sense at this point.

For example, this is very common in Swing applications, where the main method usually starts the GUI in the Swing thread, and then main ends ... but the program still works.

+22
Feb 12 '09 at 8:56
source share

You can return int with System.exit ().

Returning nothing but the whole makes little sense, since the OS expects an integer. If nothing is returned, the default value is 0, which means "OK." Other values ​​are typically used to signal errors or special conditions.

+7
Feb 12 '09 at 7:54
source share



All Articles