0

Is this possible ???

I wrote main function's return type int instead of void... class J1 { public static int main(String[] args) { System.out.println(" It works.."); return 0; } } //error occurred ....main function not found

21st Dec 2017, 4:38 AM
Gaurav Thakur
Gaurav Thakur - avatar
4 Answers
+ 3
Yes, you can but you can't run that Java class. Example class: class MainTest { public static int main(String[] args) { return 1; } } You will receive an error message when trying to run it: Error: Main method must return a value of type void in class MainTest, please define the main method as: public static void main(String[] args) Typically, in languages where main returns int (such as C and C++) the return code of main becomes the exit code of the process, which is often used by command interpreters and other external programs to determine whether the process completed successfully. To achieve the same effect in Java, use the System.exit method (analogous to the standard C function exit), like so: public static void main(String[] args) { System.exit(42); } Quoting the Java documentation linked above: Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination
21st Dec 2017, 4:47 AM
GAWEN STEASY
GAWEN STEASY - avatar
0
u cant mess with the main method because it is what the virtual machine looks for when executing code (unless you code an applet) so if u messed with it you would get the same error u just got
21st Dec 2017, 4:41 AM
kevin
0
but why i can't change the return type
21st Dec 2017, 4:43 AM
Gaurav Thakur
Gaurav Thakur - avatar
0
thanks...can u provide me the official documentation link
21st Dec 2017, 4:52 AM
Gaurav Thakur
Gaurav Thakur - avatar