Dung
Dung

Reputation: 81

Java return value

I'm C programmer. I'd like the function main return a value but apparently main is alway void. Is it true in Java?

Upvotes: 2

Views: 1386

Answers (6)

aroth
aroth

Reputation: 54806

Yes, in Java main is always public static void main(String[] args). Why would you want it to return a value anyways? It would just be returning to the enclosing java.exe process and not to the OS, which isn't very useful.

To exit and return a code to the OS, use System.exit(int code).

Upvotes: 4

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

I guess you are looking for System.exit(int retValue)

Upvotes: 2

Jim Garrison
Jim Garrison

Reputation: 86774

Use java.lang.System.exit(int status) to return a non-zero status code.

Upvotes: 3

NightDweller
NightDweller

Reputation: 923

Yes, The "main" function is the start of execution for your program, it's a "special" function which serves as the entry point to the application - you are not supposed to call it from other code and it does not return a value

Upvotes: 0

sverre
sverre

Reputation: 6919

Main always returns void in Java. If you want your program to return an error value use System.exit()

Upvotes: 17

harshit
harshit

Reputation: 7951

yes , its true , you should call the function from the main with the class object .. or declare the function as static and call from main to do the work

Upvotes: 0

Related Questions