查看java.lang.System的源码。我们能够看到System.exit()这种方法等价于Runtime.exit(),代码例如以下:
  1. /**
    * 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为何值程序都会退出,和return
相比有不同的是:return是回到上一层,而System.exit(status)是回到最上层。


System.exit(0):不是非经常见,做过swing开发的可能用过这方法,一般用于Swing窗口关闭button。(重写windowClosing方法时调用System.exit(0)来终止程序,Window类的dispose()方法仅仅是关闭窗口。并不会让程序退出)。
System.exit(1):很少见,一般在Catch块中会使用(比如使用Apache的FTPClient类时,源代码中推荐使用System.exit(1)告知连接失败),当程序会被脚本调用、父进程调用发生异常时须要通过System.exit(1)来告知操作失败,默认程序终于返回的值返是0,即然发生异常默认还是返回0,因此在这样的情况下须要手工指定返回非零。

System.exit()方法的作用的更多相关文章

  1. android中finish和system.exit方法退出的区别

    finish只是将此activity推向后台,并没有释放资源. 而system.exit则是杀死进程,会释放资源

  2. System.exit(0)作用

    System.exit(0)作用   public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0) ...

  3. main函数中System.exit()的作用

    main()主函数再熟悉不过,了解java的人也都知道System.exit()方法是停止虚拟机运行.那这里为什么还要单独写一篇博客,都是源于朋友发的一张最近刚买的T恤照片,就是上面这张图.这是一个经 ...

  4. 对于System.exit(0)和System.exit(1)的一般理解

    public static void exit(int status) 终止当前正在运行的 Java 虚拟机.参数用作状态码:根据惯例,非 0 的状态码表示异常终止. 该方法调用 Runtime 类中 ...

  5. Java 中的System.exit

    在java 中退出程序,经常会使用System.exit(1) 或 System.exit(0). 查看System.exit()方法的源码,如下 /** * Terminates the curre ...

  6. android中 System.exit(0)的理解

    public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0); } finally { Syste ...

  7. Java: System.exit() 与安全策略

    说明 System.exit() 的本质是通知 JVM 关闭. 一般来说,有两种禁用 System.exit() 的办法: 安全管理器 安全策略 本质都是JRE 提供的本地实现,在执行之前进行权限判断 ...

  8. System.exit(-1)和return 的区别

    对于只有一个单一方法的类或者系统来说是一样的,但是对于含有多个类和方法,且调用关系比较复杂时就不一样了. System.exit(-1)是指所有程序(方法,类等)停止,系统停止运行. return只是 ...

  9. android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别

    android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...

随机推荐

  1. SQL SERVER 实现分组合并实现列数据拼接

    需求场景: SQL SERVER 中组织的数据结构是一个层级关系,现在需要抓出每个组织节点以上的全部组织信息,数据示例如下: ADOrg_ID--------------ParentID------- ...

  2. Delphi 使用串口模拟工具进行串口程序开发调试

      版权声明:本文为博主原创文章,如需转载请注明出处及作者. 本文由小李专栏原创,转载需注明出处:[http://blog.csdn.net/softwave/article/details/8907 ...

  3. Delphi RichEx 图像

    unit RichEx; {2005-03-04 LiChengbinAdded:Insert bitmap or gif into RichEdit controls from source fil ...

  4. 【LeetCode 207】Course Schedule

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  5. 如何用Entity Framework 6 连接Sqlite数据库[转]

    获取Sqlite 1.可以用NuGet程序包来获取,它也会自动下载EF6 2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/do ...

  6. List转换成Json、对象集合转换Json等

    #region List转换成Json /// <summary> /// List转换成Json /// </summary> public static string Li ...

  7. 通过gdb跟踪进程调度分析进程切换的过程

    作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本实验目的:通过gdb在lin ...

  8. 过度拟合(overfitting)

    我们之前解决过一个理论问题:机器学习能不能起作用?现在来解决另一个理论问题:过度拟合. 正如之前我们看到的,很多时候我们必须进行nonlinear transform.但是我们又无法确定Q的值.Q过小 ...

  9. stl+模拟 CCF2016 4 路径解析

    // stl+模拟 CCF2016 4 路径解析 // 一开始题意理解错了.... #include <iostream> #include <string> #include ...

  10. Linux 中 x86 的内联汇编

    工程中需要用到内联汇编,找到一篇不错的文章,趁机学习下. 原文地址:http://www.ibm.com/developerworks/cn/linux/sdk/assemble/inline/ 如果 ...