just found an example on Oracle site, they use System.exit() if console is == null, but I can't see why it is better than simple return. Here's the link: http://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html
3 Answers
exit(1)
means you are exiting from System and setting exit code = 1 means an erroneous termination of the program
whereas return;
will terminate that application with exit code = 0, which to the caller means the program terminated successfully
Also See
-
1yes, I understand it in case of some relatively complicated application but using it by Oracle in such simple tutorial class seems to me a bit strange :) – skwisgaar Sep 25 '14 at 03:02
-
that is the only difference, probably the code was written from someone who came from long stay from c++ – jmj Sep 25 '14 at 03:22
System.exit(1) can be useful if you call your java program lets say from a bash script and you would like to react accordingly to success or failure of the java program.

- 116
- 4
System.exit will shutdown all the executing threads, closing files, releasing resources, etc... It terminates the JVM.
However, Return is used by an executing thread on a method, but if system.exit is used, a thread will never return. So they are two different things that are used for different purposes.
See the Java 7 Oracle document link.

- 15,342
- 16
- 114
- 137