系统类exit()方法 (System class exit() method)
exit() method is available in java.lang package.
exit()方法在java.lang包中可用。
exit() method is used to exit the currently running JVM (Java Virtual Machine).
exit()方法用于退出当前正在运行的JVM(Java虚拟机)。
exit() method is a static method, it is accessible with the class name too.
exit()方法是静态方法,也可以使用类名进行访问。
exit() method may be thrown various type of exception and the description of exception are given below,
exit()方法可能会引发各种类型的异常,下面给出了异常的描述,
SecurityException: If a particular method checkExit() does not allow exit with the given exit_status when security manager exists in the method.
SecurityException :如果特定方法checkExit()不允许在方法中存在安全管理器时使用给定的exit_status退出。
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.
exit_status –表示JVM终止的阶段或级别,此处,非零值表示JVM的异常终止。
Return value:
返回值:
The return type of this method is void, it does not return any value.
此方法的返回类型为void ,它不返回任何值。
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
翻译自: https://www.includehelp.com/java/system-class-exit-method-with-example.aspx