Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Java is a wonderful programming language having numerous applications. While programming for one of these applications you may get stuck at some juncture of this program. What does one do in this situation? Is there a way to exit at this very point? If these questions bother you, you have landed at the right place. What can you do is, simply use a System.exit() Method which terminates the current Java Virtual Machine running on the system. In this article, I will take you through exit function in Java and help you understand it thoroughly.
Let’s begin.
You can exit a function using java.lang.System.exit() method. This method terminates the currently running Java Virtual Machine(JVM). It takes an argument “status code” where a non zero status code indicates abnormal termination. If you working with Java loops or switch statements, you can use break statements which are used to break/ exit only from a loop, and not the entire program.
In this article, let’s dig deeper into Java exit() method and understand how is it used.
System.exit() method calls the exit method in class Runtime. It exits the current program by terminating Java Virtual Machine. As the method name defines, exit() method never returns anything.
The call System.exit(n) is effectively equivalent to the call:
Runtime.getRuntime().exit(n)
System.exit function has status code, which tells about the termination, such as:
Now, let’s see the parameters and the exception throws in System.exit() method.
Parameters: Exit status.
Exception: It throws a SecurityException.
Moving ahead with System.exit method(), let’s see some of its practical implementation.
package Edureka; import java.io.*; import java.util.*; public class ExampleProgram{ public static void main(String[] args) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for (int i = 0; i < arr.length; i++) { if (arr[i] >= 4) { System.out.println("Exit from the loop"); System.exit(0); // Terminates JVM } else System.out.println("arr["+i+"] = " + arr[i]); } System.out.println("End of the Program"); } }
Output: arr[0] = 1
arr[1] = 2
arr[2] = 3
Exit from the loop
Explanation: In the above program, the execution stops or exits the loop as soon as it encounters the System.exit() method. It doesn’t even print the second print statement which says the “End of the program”. It simply terminates the program there itself.
Example 2:
package Edureka; import java.io.*; import java.util.*; public class ExampleProgram{ public static void main(String[] args) { int a[]= {1,2,3,4,5,6,7,8,9,10}; for(int i=0;i<a.length;i++) { if(a[i]<=4) { System.out.println("array["+i+"]="+a[i]); } else { System.out.println("Exit from the loop"); System.exit(0); //Terminates jvm } } } }
Output: array[0]=1
array[1]=2
array[2]=3
array[3]=4
Exit from the loop
Explanation: In the above program, it prints the elements until the condition is true. As soon the condition gets false, it prints the statement and the program terminates.
Thus we have come to an end of this article on ‘exit function in Java’. I hope you understood what has been shared in this tutorial. If you wish to learn more, check out the Java Online Course by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate Spring.
If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.
Got a question for us? Please mention it in the comments section of this blog “exit function in Java” and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 19th October,2024 19th October SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co