All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Exit The Program In Java

Last Updated : Jul 1, 2023

How To Exit The Program In Java

In this article we will show you the solution of how to exit the program in java, the jump statement changes the flow of a program and transfers control from one point to another.

The Java jump statement consists of three statements: break, continue, and return.

The program can be transferred from one location to another with jump statements as well as methods such as goto() and exit().

When System.exit() is called, the Java Virtual Machine is terminated, which exits the current program.

A similar method in Java is to use gotos with labels in order to transfer control to a specific location. We will now discuss the idea of how to exit the program in java with an example.

Step By Step Guide On How To Exit The Program In Java :-

public class Talkerscode{
  public static void exampleMethod(int[] array1) {
    for (int i = 0; i < array1.length; i++) {
      if (i > 4) {
        System.out.println("Terminating JVM.");
        System.exit(0);
      }
      System.out.println("Array Index: " + i + " Array Element: " + array1[i]);
    }
  }
  public static void main(String[] args) {
    int[] array1 = { 0, 2, 4, 6, 8, 10, 12, 14, 16 };
    exampleMethod(array1);
  }
}
  1. Our first step is to define a class named "Talkerscode" that is public.
  2. An array of integers is passed as the parameter of the method "exampleMethod" defined within this class.
  3. Array elements are iterated through using the "for" loop.
  4. On the console screen, a message ("Terminating JVM") is printed if the current element's index is greater than 4.
  5. Our program is terminated by exiting the Java Virtual Machine (JVM) using the "System.exit(0)" method.
  6. On the console, the array index and value are printed if the current element has an index below or equal to 4.
  7. An integer array "array1" is created and initialized with the values 0, 2, 4, 6, 8, 10, 12, 14, and 16 in "main" method.
  8. To end our example, we call the exampleMethod() method and pass "array1" as an argument.
  9. Based on the index of the elements of "array1", the exampleMethod() method iterates through them and prints their values or terminates the program.

Conclusion :-

As a result, we have successfully learned how to exit the program in java with an example.

In addition to terminating the Java Virtual Machine, System.exit( ) also terminates the process that is currently running.

A parameter within the exit () function is passed to the operating system.

Anywhere in the java program where system.exit() is needed can be used depending on the logic of the program and the objective it is trying to accomplish.

I hope this article on how to exit the program in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪