Exit Method in Java
Jump statements are used to change the flow of a program and transfer control from one point to another point. The break, continue and return statements are the jump statements used in Java.
Along with jump statements, methods like goto() and exit() are used to transfer the control of the program. System.exit() method exits current program by terminating the Java Virtual Machine. Similarly goto with label in Java is used to transfer the control of the program to a particular position in the code.
Java has provided a number of inbuilt functions and statements that can control and transfer the flow of programs. Jump statements like break, continue, and return are used in loops and functions. Java does not support goto and has reserved it as a keyword to use in later versions, but instead, it supports label.
- goto(label) is a control statement used to transfer control to a certain point in the program.
- goto statement works with the labels which serve as identifiers and are used to a point to a particular position in the code.
- We can use label name with break statement to break out of a specific outer loop.
- We can use label name with continue statement to continue execution from a particular point in the code.
- After the goto(label) statement is executed program starts executing the lines where goto(label) has transferred its control and then subsequent statements are executed.
When the if condition is true, i.e. when j == 2, break Task; will break out of the outermost loop labelled with Task.
What is exit() Method in Java?
Java provides exit() method to exit the program at any point by terminating running JVM, based on some condition or programming logic. In Java exit() method is in java.lang.System class. This System.exit() method terminates the current JVM running on the system which results in termination of code being executed currently. This method takes status code as an argument.
- exit() method is required when there is an abnormal condition and we need to terminate the program immediately.
- exit() method can be used instead of throwing an exception and providing the user a script that mentions what caused the program to exit abruptly.
- Another use-case for exit() method in java, is if a programmer wants to terminate the execution of the program in case of wrong inputs.
Syntax of exit() in Java
Parameters of exit() in jJava
System.exit() method takes status as a parameter, these status codes tell about the status of termination. This status code is an integer value and its meanings are as follows:
- exit(0) - Indicates successful termination
- exit(1) - Indicates unsuccessful termination
- exit(-1) - Indicates unsuccessful termination with Exception
Note: Any non-zero value as status code indicates unsuccessful termination.
Return Value for exit() in Java
As the syntax suggests, it is a void method that does not return any value.
Exit a Java Method using System.exit(0)
As mentioned earlier, System.exit(0) method terminates JVM which results in termination of the currently running program too. Status is the single parameter that the method takes. If the status is 0, it indicates the termination is successful.
Let's see practical implementations of System.exit(0) method in Java.
Here is a simple program that uses System.exit(0) method to terminate the program based on the condition.
In the above program, an array is being accessed till its index is 4. Until if condition is false i.e., array index <= 4, JVM will give Array index and element stored at that index as the output.
Once the if condition is true i.e., array index > 4, JVM will execute statements inside the if condition. Here it will first execute the print statement and when exit(0) method is called it will terminate JVM and program execution.
Let's see one more example of exit() method in a try-catch-finally block.
The piece of code above is trying to read a file and print a line from it if the file exists. If a file doesn't exist, the exception is raised and execution will go in catch block and code exits with System.exit(0) method in the catch block.
- We know, finally block is executed every time irrespective of try-catch blocks are being executed or not. But, in this case, the file is not present so JVM will check the catch block where System.exit(0) method is used that terminates JVM and currently running the program.
- As the JVM is terminated in the catch block, JVM won't be able to read the final block hence, the final block will not be executed.
Exit a Java Method using Return
exit() method in java is the simplest way to terminate the program in abnormal conditions. There is one more way to exit the java program using return keyword. return keyword completes execution of the method when used and returns the value from the function. The return keyword can be used to exit any method when it doesn't return any value.
Let's see this using an example:
In the above program, SubMethod takes two arguments num1 and num2, subtracts num2 from num1 giving the result in the answer variable. But it has one condition that states num2 should not be greater than num1, this condition is implemented using if conditional block.
If this condition is true, it will execute if block which has a return statement. In this case, SubMethod is of void type hence, return doesn't return any value and in this way exits the function.
Does goto Exist in Java?
No, goto does not exist in Java. Because Java has reserved it for now and may use it in a later version.
In other languages, goto is a control statement used to transfer control to a certain point in the programming. After goto statement is executed program starts executing the lines where goto has transferred its control and then other lines are executed. goto statement works with labels that are identifiers, these labels are used to the state where goto has to transfer the control of execution.
Java does not support goto() method but it supports label. No support for goto method is due to the following reasons:
- Goto-ridden code hard to understand and hard to maintain
- These labels can be used with nested loops. A combination of break, continue and labels can be used to achieve the functionality of goto() method in Java.
- Prohibits compiler optimization
In the above code, the outer label is created for the first for loop. When if the condition is true break outer; statement will go back to first for loop and break the execution. Use this Compiler to compile your Java code.
- exit() method is used to terminate JVM.
- Status code other than 0 in exit() method indicates abnormal termination of code.
- goto does not exist in Java, but it supports labels.
- It is better to use exception handling or plain return statements to exit a program while execution.
- System.exit() method suit better for script-based applications or wherever the status codes are interpreted.