查看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. hdu 2870(dp求最大子矩阵)

    题意:让你求的是由同一字母组成的最大子矩阵,w可以变成a或者b,x可以变成b或者c,y可以变成a或者c,z可以变成a或者b或者c. 分析:这是hdu 1506.hdu 1505的加强版,具体的分析看我 ...

  2. EF6 Database First (DbContext) - Change Schema at runtime

    Problem:There are two SQL databases (dev and live) with on Azure which has identical table structure ...

  3. node.js study: cluster

    从v0.6.x开始,Node.js提供了多进程模块cluster,允许创建一组进程来共享同一个socket,并且分担负载压力.官方文档是这样说的:A single instance of Node.j ...

  4. duilib relativepos属性导致控件错误的bug修复

    转载请说明出处,谢谢~~ 我在仿酷狗音乐播放器的开发日志系列里,曾经提到了这个bug,文章地址为:http://blog.csdn.net/zhuhongshu/article/details/381 ...

  5. SQL你必须知道的-查询聚合分组排序

    use MySchoolTwo    -- 简单查询    select * from Student    -- 话说这种查询的效率要比 * 要高级点    select sId , sName , ...

  6. Text Kit进阶

    在上一篇文章Text Kit入门中我们主要了解了什么是Text Kit及它的一些架构和基本特性,这篇文章中会涉及关于Text Kit的更多具体应用. Text Kit是建立在Core Text框架上的 ...

  7. 面向切面编程(AOP)及其作用

    在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用. 1.面向切面编程(AOP) 面向切面编程(AOP)就是对软件系统不同关注点的分离,开发者通过拦截方法调用并在方法调用前后添加辅助代码. ...

  8. SSH整合常见错误

    spring+hibernate出错小结: (1)java.lang.NoClassDefFoundError: org/hibernate/context/CurrentSessionContext ...

  9. 一些不错的英文歌曲MV,留个存档!

    Lambada [[http://www.yinyuetai.com/video/265213]]Trouble Is A Friend [[http://www.yinyuetai.com/vide ...

  10. Core Java 学习笔记——1.术语/环境配置/Eclipse汉化字体快捷键/API文档

    今天起开始学习Java,学习用书为Core Java.之前有过C的经验.准备把自己学习这一本书时的各种想法,不易理解的,重要的都记录下来.希望以后回顾起来能温故知新吧.也希望自己能够坚持把自己学习这本 ...