Hii Folks,
As everyone knows, Java is a multi-platform, object-oriented programming language that runs on billions of devices worldwide. and Today, I'm going to write about one of its methods which is System.exit()
. In an earlier article, I wrote about String Data Type. In that blog post, I explained the basic concept of String Data Type and Its methods. Now I'm going to write about the System.exit()
method.
The System
class in Java contains methods to manage the system. One of these is the System.exit()
method used in Java when you want to end a program or the current JVM instance. Any code placed after the exit method will be unreachable and won't run.
In Java, There are two ways to terminate a Java program:
We can terminate the Java program using either the System.exit()
method or the return statement.
The System.exit()
method in Java is used to terminate the currently running Java Virtual Machine (JVM). It takes an integer parameter that specifies the exit status of the program. The exit status is the value that is returned to the operating system when the program terminates.
Syntax of System.exit() method
The syntax of the System.exit()
method is:
public void static(int status)
You can observe that it's a static method. All methods in the System class are static. The exit()
method takes an integer as an argument and doesn't return anything. You would use the exit method like this: System.exit(i)
, where i is an integer. This integer is known as the "exit status" and can be either zero or a non-zero value.
System.exit(0)
method is generally used for successful termination.
System.exit(-1)
the method generally used for unsuccessful termination with Exception.
System.exit(1)
indicates abnormal termination of the JVM.
Example of System.exit() method
Let’s look at two simple examples of the exit() method with status as zero and non-zero integers. In our first example, there is a loop over an array of colours. When the loop encounters "green", the application needs to terminate.
The output will be displayed
The terminal didn’t display any exit code in the output because we used zero as the status. Since zero indicates successful termination, there's no need to show an exit code. Let's try using a positive integer as the status in our next example.
In this example, we have a loop that produces random numbers between 0 and 10. If the generated number is 2, 5, or 7, the application should terminate, displaying the number that caused the termination. Below is the code snippet.
When we executed the code, we got
As you can see, number 5 caused the abnormal termination of the application. Now, let's explore how the status code can be utilized effectively.
How the status code can be utilized effectively
Status codes are crucial when running a Java program via a command-line interface (CLI). They are helpful if you plan to connect this program with other standard tools, programs, or operating systems. When communicating with an operating system, you can employ status codes tailored to that system. For instance, in UNIX, 128 is the typical status code indicating an “Invalid argument to exit.”
The System.exit()
method can be used in any Java program, regardless of the type of program. However, it is most commonly used in console applications and GUI applications.
When can we use the System.exit() method?
The "System.exit()"
method is commonly used when a program needs to be terminated immediately due to an abnormal condition. This might happen if the program encounters an unexpected error or if the user requests to exit the program.
In Java, there is a special construct called "Shut Down Hooks", which allows developers to insert a code snippet that must be executed before the termination of JVM. These hooks are particularly helpful in performing clean-up operations. In such cases, the System.exit
method is used to call the shutdown hooks.
How can we terminate the Java program using the return statement?
The return statement can also be used to terminate a Java program. When a return statement is executed, the current method returns to the calling method. If the current method is the main() method, then the program terminates.
Here is an example of how to use the return statement to terminate a Java program:
When this code is executed, the main() method will return immediately and the program will terminate.
Sometimes, it becomes necessary to use the return statement to terminate a Java program. This is particularly useful when a program is running in a thread, and you need to terminate the thread. In such cases, the return statement can be used to achieve this purpose.
In general, it is preferred to use the System.exit()
method to terminate a Java program. This is because the System.exit()
method ensures that all resources are properly cleaned up before the program terminates.
Important Notes
TheSystem.exit()
method and the return statement are not the same thing. TheSystem.exit()
method terminates the JVM, while the return statement simply returns to the calling method.
TheSystem.exit()
method does not return any value. Once theSystem.exit()
method is called, the JVM is terminated and the program exits. Any code that is written after theSystem.exit()
method call will not be executed.
Conclusion
In this article, we have explored the method System.exit()
in Java programming. Additionally, we have discussed two ways to terminate a Java program, namely via the return statement and the System.exit()
method. It is important to note that the System.exit()
method is specifically used to terminate the JVM in Java. Once this method is called, the application will not execute any code beyond it. We have also explored the real-world uses of the exit()
method