Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return type of main in java

Tags:

java

I like to know why only void return type for main method in java.

public static void main(String [] args)

Why there is no other return types other than void for main method.

Thanks

like image 738
giri Avatar asked Mar 14 '10 03:03

giri


People also ask

What is the return type of main?

The return value of main() function shows how the program exited. The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value. In C++ language, the main() function can be left without return value.

Can return type of main be int in java?

Answer: Explanation: True, The default return type for a function is int.

Why Main is static in java?

Java main() method is always static, so that compiler can call it without the creation of an object or before the creation of an object of the class. In any Java program, the main() method is the starting point from where compiler starts program execution. So, the compiler needs to call the main() method.

How do you return a value to the main in java?

Just cancel first call for getMinutes() method in your main , each time you call or try to get return value of a method . this method will execute all codes inside it before you got its return value.


2 Answers

What were you expecting to return?

In C you typically return an int exit code. However, in a multithreaded system this doesn't make much sense. Typically a Java program will start a number of threads. The main thread will often return more or less immediately.

System.exit can be used to exit with a specific exit code.

like image 36
Tom Hawtin - tackline Avatar answered Oct 21 '22 16:10

Tom Hawtin - tackline


The short answer is: Because that's what the language specification says.

In today's commonly used operating systems (Windows and Unix families), the only "return value" from a process is an exit status, which is a small number (usually 8-bit). This value can be returned from a Java program using the System.exit(int) method:

public static void exit(int status)

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

like image 118
Greg Hewgill Avatar answered Oct 21 '22 15:10

Greg Hewgill