I found out ways to terminate (shut-down or stop) my Java programs. I found two solutions for it.
using return;
When I want to quit or terminate my program execution , I add this.
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) ?
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.
return; in your main() method.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With