aspose file tools*
The moose likes Beginning Java and the fly likes Why should the return type of main() be void Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why should the return type of main() be void" Watch "Why should the return type of main() be void" New topic
Author

Why should the return type of main() be void

Hari Mohan
Greenhorn

Joined: Jul 30, 2008
Posts: 7
Why should the return type of a main method be void?
Per my understanding, even methods with a return type can be called without assigning the (returned) result to a reference(or primitive) variable.......(see below)..So, why can't I declare my main() with some int return type------- public static int main().


Now, on running this program, I get a "Hello HMV" printed on my console......................
what I intend to ask is,
if the JVM gives a main method call like main(args); in it's implementation, this should as well work fine for int return types. But it looks at public static int main(String args[]) as just another method whose entry point, the JVM doesn't know......
What exactly happens in the JVM that it looks for a void return type only.

[ July 30, 2008: Message edited by: Hari --------- ]
[ July 30, 2008: Message edited by: Hari V ]
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
A method with same name and parameters but with different returning type is NOT an overloaded method ... you shouldn't be allowed to define it either, you'd get compilation error.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hari ---------, please check your private messages.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Hari Mohan
Greenhorn

Joined: Jul 30, 2008
Posts: 7
Well, That's fine......sorry for that......editing the post......but, that doesn't effect it.....
what I meant was that the JVM would look at public static int main() as some method with name main that returns an int.....So, why cant it execute it.......if it can work for a signature main(args), why does it care for the return type.............
Let me elucidate, in the code snippet, even if my printName were having a returntype void......the code in main works.......it would still print "Hello HMV".....
Any method with a returntype works just as fine as it would work with a void return type if the same call were given (like the printName("HMV") in the code snippet)....
[ July 30, 2008: Message edited by: Hari V ]
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
The main method must follow specific rules;


12.1.4 Invoke Test.main
Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the method main of Test is invoked.
The method main must be declared public, static, and void. It must accept a single argument that is an array of strings. This method can be declared as either


public static void main(String[] args)

or

public static void main(String... args)


This is followed by the JVM.

The main method may throw Exceptions.


If you want to return a value to the system, pass it to void System.exit(int).


Java hobbyist.
Hari Mohan
Greenhorn

Joined: Jul 30, 2008
Posts: 7
Thanks for that Ronald..........
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
I think it's just a matter of conventions. What define a method in Java is just the method name and the parameter list (number and type of parameters, in exact order). Thus :



and



are just the same method (that's why you get e compile error if you define both of them).
The fact that JVM calls exactly the method main (args[]) returning a void type it's just a fact of ... convention ! They could have defined any other signature to be the default method called by the JVM, such as



Once again, the returning type is not relevant at all, what makes the difference is method name and params (again, number and types in exact order). What you do with the returned value, assigning it to a variable or simply NOT assignigning it, like when you just call :



it's not a matter of the JVM.
[ July 30, 2008: Message edited by: Matteo Di Furia ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

The int in C/C++ is used for passing return codes to the operating system; usually 0 means success and any other value means some kind of error.

The Java creators chose to use System.exit(int code) for that purpose. It also allows you to return this value from anywhere in your application, not just in the main method.

And Hari V, we urge you to read your private messages. Failure to do so may lead to your account being removed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Hari Mohan
Greenhorn

Joined: Jul 30, 2008
Posts: 7
Originally posted by Rob Prime:
The int in C/C++ is used for passing return codes to the operating system; usually 0 means success and any other value means some kind of error.

The Java creators chose to use System.exit(int code) for that purpose. It also allows you to return this value from anywhere in your application, not just in the main method.

And Hari V, we urge you to read your private messages. Failure to do so may lead to your account being removed.


Sorry for the violation Rob, changed the name to one adhering policy....

Well coming to returning an int, I was speaking about returning anything at all maybe int, maybe char......
I mean, what's the trouble in returning a char or a double or whatever when it is waiting for no return.....after all, I return a char and no one catches it....what's the trouble.......I understand that JVM is looking for a signature of a method which returns nothing...And that's the rule........But I don't understand why.......
Please correct me....Per what some friend said,
public char void main(String args[]) essentially ends by giving System.exit(char code) at the end of the method......So, any other Threads that (in case) have been started tend to exit abruptly.......whereas, public static void main(String args[]) doesn't call System.exit()......Does that make any sense.....
[ July 31, 2008: Message edited by: Hari Mohan ]
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
Actually not. Suppose you could make a


you could exit from it even with



while, on the other end, in your



you're allowed to exit with



Since you're running an application through its main method, when main has done what it has to do, it's safe to exit with (or without) System.exit, there'll be nothing after the completion of the main method (except operations made by the JVM).
And, by the way, with System.exit(1) you're just exiting your program with an error code, but you're not making the main method to return an int (in this case, 1), they are completely different things.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why should the return type of main() be void