What happens.when we write "public static int main (String args [])" instead of "void main" in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What happens.when we write "public static int main (String args [])" instead of "void main" in java

8th Jun 2019, 5:28 PM
Aadarsh Gupta
Aadarsh Gupta - avatar
5 Answers
+ 3
You get a compile time error that says you must return type void from main method
8th Jun 2019, 5:32 PM
Robert Atkins
Robert Atkins - avatar
+ 1
But why it should always return null value
8th Jun 2019, 5:37 PM
Aadarsh Gupta
Aadarsh Gupta - avatar
+ 1
Aadarsh Gupta actually when using the "void" keyword nothing at all is returned, control is just restored to the main program that invoked our method, in this case the operating system.
8th Jun 2019, 6:11 PM
Robert Atkins
Robert Atkins - avatar
+ 1
So there is any way to return a value other than null
8th Jun 2019, 6:15 PM
Aadarsh Gupta
Aadarsh Gupta - avatar
+ 1
Aadarsh Gupta Not from the "main" method however if you write your own methods that you call within the "main" method you can return any datatype from them that you need to.
8th Jun 2019, 6:21 PM
Robert Atkins
Robert Atkins - avatar