查看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 5387 Clock

    题意:给一个时间,求三个时针之间的夹角,分数表示. 解法:算算算.统一了一下分母. 代码: #include<stdio.h> #include<iostream> #incl ...

  2. [Papers]NSE, $\n u_3$, Lebesgue space, [Pokorny, EJDE, 2003; Zhou, MAA, 2002]

    $$\bex \n u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{3}{2},\quad 2\leq q\leq \i ...

  3. Android selector item 属性大全(按钮按下不同效果)

    <selector>         必须.必须是根元素.包含一个或多个<item>元素.          Attributes:             xmlns:and ...

  4. HDU5697 刷题计划 dp+最小乘积生成树

    分析:就是不断递归寻找靠近边界的最优解 学习博客(必须先看这个): 1:http://www.cnblogs.com/autsky-jadek/p/3959446.html 2:http://blog ...

  5. XTUOJ 1252 Defense Tower 贪心

    题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1252 思路:考虑每条边对玩家的伤害 假设连接的节点是u,v,破坏 ...

  6. 【和我一起学python吧】python的数据类型

    python的元组.列表.字典数据类型是很python(there python is a adjective )的数据结构.这些结构都是经过足够优化后的,所以如果使用好的话,在某些area会有很大的 ...

  7. 【转】JS设计模式开篇

        (原文地址:http://blog.chinaunix.net/uid-26672038-id-3904513.html)     本文主要讲述一下,什么是设计模式(Design patter ...

  8. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  9. 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇02:滚屏》

    2.滚屏 滚屏概述: 打飞机游戏场景背景设计通常很简单,因为角色敌人道具等都不与背景发生交互事件.开发者只需要根据设定的游戏类型,为游戏制作背景,模拟一个大环境即可. 滚屏原理: 材质UV动画,实现背 ...

  10. 对ArrayList 进行深拷贝

    ArrayList arr = new ArrayList(); arr.Add()); arr.Add()); arr.Add()); ArrayList arr2 = new ArrayList( ...