Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How do you exit a function in Java?

Last updated on Nov 29,2022 51.4K Views

Aayushi Johari
A technophile with a passion for unraveling the intricate tapestry of the... A technophile with a passion for unraveling the intricate tapestry of the tech world. I've spent over a decade exploring the fascinating world of...
63 / 72 Blog from Java Core Concepts

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.

How do you exit a function in Java?

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.

What is System.exit() Method?

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:

  • exit(0) : Indicates successful termination.
  • exit(1) or exit(-1) or any non-zero value – indicates unsuccessful termination.

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.

Java System exit() Method Examples


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.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How do you exit a function in Java?

edureka.co