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
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.
Answer: Explanation: True, The default return type for a function is int.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With