tags:

views:

490

answers:

7

Hi,

With respect to C/C++ main() must always return an integer(zero to indicate success and non-zero to indicate failure). I can understand this as the program is run it becomes a process and every process should have an exit status, which we obtain by doing echo $? from shell after the process gets over.

Now I don't understand why is the main method does not return anything in Java? Has it got anything to do with the fact that the program is run on JVM and the JVM process is reposnsible for the returning of exit status?

Please clarify.

Thanks,
Roger

+7  A: 

Java has System.exit(int) for that purpose.

Basically, a Java program will exit with exit code 0, if program flow reaches the end of the main method (roughly; it gets weirder with Swing and threading, but this should suffice).

If you stick a call to System.exit() somewhere (anywhere, actually) or Runtime.getRuntime().exit() which is what System.exit() calls, then the program ends immediately and prematurely.

You could picture it like Java would implicitly add System.exit(0) at the very end of the main method but there may be more subtle differences. I don't have the complete details of JVM shutdown in my head right now, though.

Joey
We don't write that at the end of every Java program, do we?
gameover
If you don't wish to return abnormally, no. The implicit exit code is `0`, as usual.
Joey
You mean to say if I don't have a System.exit() in my program, JVM will sort of add it with 0. And if my program has an explicit call with non-zero value, it will be used as exit status ?
codaddict
codaddict: Roughly, yes. I expanded my answer.
Joey
A: 

Does System.exit(int exitCode) not return a value?

DerMike
+11  A: 

If the main method of a single threaded java application terminates, the application will terminate with exit code 0. If you need another exit code, maybe to indicate an error, you can place

System.exit(yourNumberHere);

anywhere in the code (especially outside of the main method).

This is different for multi-threaded applications , where you either have to use System.exit from the inside of kill -9 from the outside to stop the JVM.

Here's a quick example where termination of main doesn't stop the application (a typical service or daemon behaviour):

public static void main(String args[]) {  
  Thread iWillSurvive = new Thread(new Runnable() {
    public void run() {
      while(true) {
        // heat the CPU
      }
    }
  });
  iWillSurvive.start();
}

Remark: Sure, a thread will terminate when it's run method (or the main method in case of the main thread) terminates. And in this case, when all threads have terminated, the JVM will terminate with exit code 0 (which brings us back to the initial question). Hope everybody is happy now.

Andreas_D
You mean to say if I don't have a System.exit() in my program, JVM will sort of add it with 0. And if my program has an explicit call with non-zero value, it will be used as exit status ?
gameover
The Java process does _not_ necessarily terminate if the main method terminates, making it (as kai1968 points out) unreasonable to return the process' exit code from the main method.
jarnbjo
Still wrong. It's not necessary to use tools like kill or System.exit to make a multithreaded Java application stop. Hint: Even the simplest "Hello world" Java application is mutlithreaded (the Java VM starts its own threads for different management issues) and it exits, when the main method is done.
jarnbjo
jarnbjo - I think it's enough know. Mentioning 'kill' (in a wrong syntax by the way, because you have to identify the process) was intended to be a humerous addition. OF COURSE you can an should design a thread in a way that it either terminates or can be terminated in a proper way. (But I'll strike out this part, if it makes you happier)
Andreas_D
+14  A: 

Designed when multi-threading was already a common thing, java said (by design) "good bye" to the idea that when 'main' returns the program is done. That's why there's no return value. As the others said use System.exit when you want to exit with return code.

Kai
That's pretty much it in a nutshell.
McDowell
+1 great explanation :)
atv
+4  A: 

A minor niggle - in C++, you do not need to return anything from main:

int main() {
}

is a perfectly legal C++ program - it acts as if you had:

return 0;

as the last statement in main.

anon
I came here to say this. It always suprises me that the standard doesn't require you to return void in this case, and in fact you are not allowed to declare it that way. But that's how it works
John Burton
A: 

I suspect the reason for C/C++ returning int is because that's the convention for indicating function/method/program success/failure in those language.

With the introduction of Exceptions in Java, instead of zero for success and non-zero values for failure, it uses an exception hierarchy for failures, and successes were simply when no exceptions were thrown, thus eliminating the need to return a value.

Jack Leow
Except that throwing an exception from `main()` is not going to be useful, and the only output has to be a return code. The fact that C doesn't support exceptions in a C program and Java does in a Java program doesn't affect how these interact with the operating system.
David Thornley
A: 

Most programs I write these days have some form of user interface and little that they could usefully return, and even less likelyhood that anything would every need that status code.

So why force all programs to return one? As others have said, you can return a status code should you wish to...

John Burton
What is forcing programs to return a status code?
Joe Gauterin