查看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. js函数——倒计时模块+无缝滚动

    倒计时 效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  2. POJ 1064 Cable master

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37865   Accepted: 8051 Des ...

  3. [Everyday Mathematics]20150205

    设 $\phi:[k_0,\infty)\to[0,\infty)$ 是有界递减函数, 并且 $$\bex \phi(k)\leq \sex{\frac{A}{h-k}}^\al\phi(h)^\be ...

  4. Tombstone crash

    首先,android平台应用程序可能产生以下四种crash:App层:Force close crashANR crashNative层:Tombstone crashKernel层:Kernel p ...

  5. Ye.云狐J2刷机笔记 | 完美切换内部存储卡和SD卡的改法.vold.fstab

    ================================================================================Ye.完美切换内部存储卡和SD卡成功.v ...

  6. linux下mysql数据库的学习

    转载博客:http://freedomljtt.blog.163.com/blog/static/72294949201210145441701/ ubuntu12.04 卸载和安装mysql 卸载m ...

  7. CATALOGUE 目录

    1 语言基础 1.1 c/c++ [转]C/C++ 存储类型 作用域 连接类型 [转]C/C++内存划分 [转]C/C++除法实现方式及负数取模详解 [转]为什么C++编译器不能支持对模板的分离式编译 ...

  8. 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point

    // 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...

  9. C++读取、旋转和保存bmp图像文件编程实现

    以前也遇到过bmp文件的读写.这篇博客很好,写的其他内容也值得学习. 参考:http://blog.csdn.net/xiajun07061225/article/details/6633938  学 ...

  10. PhoneGap,Cordova[3.5.0-0.2.6]:生成Android项目时出现错误(An error occurred while listing Android targets)

    我在升级到Cordova最新版本(3.5.0-0.2.6)后,在生成Android项目(cordova platform add android)时出现错误: Error: An error occu ...