Home » Java programming language

Java System class exit() method with example

System class exit() method: Here, we are going to learn about the exit() method of System class with its syntax and example.
Submitted by Preeti Jain, on September 16, 2019

System class exit() method

  • exit() method is available in java.lang package.
  • exit() method is used to exit the currently running JVM (Java Virtual Machine).
  • exit() method is a static method, it is accessible with the class name too.
  • exit() method may be thrown various type of exception and the description of exception are given below,
    SecurityException: If a particular method checkExit() does not allow exit with the given exit_status when security manager exists in the method.

Syntax:

    public static void exit (int exit_status);

Parameter(s):

  • exit_status – represents the stages or level of termination of JVM, and here, the non-zero value represents the abnormal termination of JVM.

Return value:

The return type of this method is void, it does not return any value.

Example:

// Java program to demonstrate the example of 
// exit() method of System Class

public class ExitMethod {
    public static void main(String[] args) {
        // declaring an array
        int array[] = {
            10,
            20,
            30,
            40,
            50
        };

        for (int i = 0; i < array.length; ++i) {
            if (array[i] < 60) {
                System.out.println("Element at index" + " " + i + " is " + array[i]);
            } else {
                System.out.println("We are exiting JVM normally");
                System.exit(0);
            }
        }

    }
}

Output

E:\Programs>javac ExitMethod.java
E:\Programs>java ExitMethod
Element at index 0 is 10
Element at index 1 is 20
Element at index 2 is 30
Element at index 3 is 40
Element at index 4 is 50





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.