Java千百问_03基础语法(015)_System.exit(0)有什么用

来源:互联网 发布:用java打印等腰三角形 编辑:程序博客网 时间:2024/05/21 07:01

点击进入_更多_Java千百问

1、System.exit(0)有什么用

查看java.lang.System的源代码,我们可以找到System.exit(status)这个方法,源代码如下:

public final class System {/**    * Terminates the currently running Java Virtual Machine. The    * argument serves as a status code; by convention, a nonzero status    * code indicates abnormal termination.    * <p>    * This method calls the <code>exit</code> method in class    * <code>Runtime</code>. This method never returns normally.    * <p>    * The call <code>System.exit(n)</code> is effectively equivalent to    * the call:    * <blockquote><pre>    * Runtime.getRuntime().exit(n)    * </pre></blockquote>    *    * @param      status  exit status.    * @throws  SecurityException    *        if a security manager exists and its <code>checkExit</code>    *        method doesn't allow exit with the specified status.    * @see        java.lang.Runtime#exit(int)    */    public static void exit(int status) {        Runtime.getRuntime().exit(status);    }}

注释中说的很清楚,这个方法是用来结束当前正在运行中的java虚拟机。参数status作为状态码,按照惯例,一个非零状态码表示异常,所以说status如果是非零的,那么表示是非正常退出。调用System.exit(n)实际上相当于调用:

Runtime.getRuntime().exit(n);

对于System.exit(0)来说,有以下几点需要注意:

  1. System.exit(0)是将整个虚拟机都停掉了 ,也就是说连内存也都被释放了。而dispose()只是关闭这个窗口,但是并没有停止整个应用。

  2. System.exit(0)是正常退出程序,而System.exit(1)或者说status非0表示非正常退出程序。

  3. 不管status为何值都会退出程序

1 0
原创粉丝点击