101

I found out ways to terminate (shut-down or stop) my Java programs. I found two solutions for it.

  1. using return;
    When I want to quit or terminate my program execution , I add this.

  2. using System.exit() ;
    Sometimes I used it. I read about System.exit() from this question.

So, I know a little on both them. But I am still confused as to how they actually work. Please check below codes...

public class Testing {

public static void main(String... str) {
    System.out.println(1);
    System.exit(0);
    System.out.println(2);
    return;
 }
}

I am sure that 2 will not appear. I would like to know is why return; or other codes can write below the statement of System.exit(0); and what was real definition for return; (because it is strange thing for me return without any variables or values) ?

Community
  • 1
  • 1
Cataclysm
  • 7,592
  • 21
  • 74
  • 123
  • possible duplicate of http://stackoverflow.com/questions/11487184/why-is-return-needed-even-after-system-exit0 – RKC Mar 17 '14 at 11:23
  • possible duplicate of [How to quit a java app from within the program](http://stackoverflow.com/questions/2670956/how-to-quit-a-java-app-from-within-the-program) – Suma Jan 09 '15 at 10:46

7 Answers7

127

Calling System.exit(0) (or any other value for that matter) causes the Java virtual machine to exit, terminating the current process. The parameter you pass will be the return value that the java process will return to the operating system. You can make this call from anywhere in your program - and the result will always be the same - JVM terminates. As this is simply calling a static method in System class, the compiler does not know what it will do - and hence does not complain about unreachable code.

return statement simply aborts execution of the current method. It literally means return the control to the calling method. If the method is declared as void (as in your example), then you do not need to specify a value, as you'd need to return void. If the method is declared to return a particular type, then you must specify the value to return - and this value must be of the specified type.

return would cause the program to exit only if it's inside the main method of the main class being execute. If you try to put code after it, the compiler will complain about unreachable code, for example:

public static void main(String... str) {
    System.out.println(1);
    return;
    System.out.println(2);
    System.exit(0);
}

will not compile with most compiler - producing unreachable code error pointing to the second System.out.println call.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • `return would cause the program to exit only if it's inside the main method of the main class being execute.` that is not true. `return` from the main class will terminate the current execution context. The Java application is exists when there are no (non-daemon) active threads running. So if you will create a thread in this main and run it, calling `return` won't exist the application. – Roee Gavirel Oct 04 '21 at 06:34
  • I was running a jar file in a debian OS, and even after System.exit(), the bash file would not close. – X Builder Aug 09 '22 at 16:10
19
  1. System.exit() is a method that causes JVM to exit.
  2. return just returns the control to calling function.
  3. return 8 will return control and value 8 to calling method.
prady00
  • 721
  • 1
  • 6
  • 17
13

Because System.exit() is just another method to the compiler. It doesn't read ahead and figure out that the whole program will quit at that point (the JVM quits). Your OS or shell can read the integer that is passed back in the System.exit() method. It is standard for 0 to mean "program quit and everything went OK" and any other value to notify an error occurred. It is up to the developer to document these return values for any users.

return on the other hand is a reserved key word that the compiler knows well. return returns a value and ends the current function's run moving back up the stack to the function that invoked it (if any). In your code above it returns void as you have not supplied anything to return.

indivisible
  • 4,892
  • 4
  • 31
  • 50
7

System.exit() terminates the JVM. Nothing after System.exit() is executed. Return is generally used for exiting a method. If the return type is void, then you could use return; But I don't think is a good practice to do it in the main method. You don't have to do anything for terminate a program, unless infinite loop or some strange other execution flows.

Karura91
  • 603
  • 6
  • 15
7

So return; does not really terminate your java program, it only terminates your java function (void). Because in your case the function is the main function of your application, return; will also terminate the application. But the return; in your example is useless, because the function will terminate directly after this return anyway...

The System.exit() will completly terminate your program and it will close any open window.

ReeCube
  • 2,545
  • 17
  • 23
5
  • Well, first System.exit(0) is used to terminate the program and having statements below it is not correct, although the compiler does not throw any errors.
  • a plain return; is used in a method of void return type to return the control of execution to its parent method.
anirudh
  • 4,116
  • 2
  • 20
  • 35
  • That's really odd. I'd have expected the compiler to throw an "Unreachable code" error. – user3932000 Jan 28 '17 at 00:38
  • The compiler doesn't know what System.exit(0) does (nor should it make such an assumption, since you could technically do some ClassLoader magic to load up a different implementation of System.exit() that doesn't cause it exit), – David Liu Oct 15 '18 at 03:06
  • Yeah I guess you're right. Maybe a warning would make sense. – anirudh Oct 15 '18 at 08:11
4

What does return; mean?

return; really means it returns nothing void. That's it.

why return; or other codes can write below the statement of System.exit(0);

It is allowed since compiler doesn't know calling System.exit(0) will terminate the JVM. The compiler will just give a warning - unnecessary return statement

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53