2.解析

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

  /**
* 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是非零参数,那么表示是非正常退出。

  1. System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() 。无论如何,内存都释放了!也就是说连JVM都关闭了,内存里根本不可能还有什么东西
  2. System.exit(0)是正常退出程序,而System.exit(1)或者说非0表示非正常退出程序
  3. System.exit(status)不管status为何值都会退出程序。和return 相比有以下不同点:return是回到上一层,而System.exit(status)是回到最上层

3.示例

在一个if-else判断中,如果我们程序是按照我们预想的执行,到最后我们需要停止程序,那么我们使用System.exit(0),而System.exit(1)一般放在catch块中,当捕获到异常,需要停止程序,我们使用System.exit(1)。这个status=1是用来表示这个程序是非正常退出。

http://www.cnblogs.com/xwdreamer/archive/2011/01/07/2297045.html

system.exit(0) vs system.exit(1)的更多相关文章

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

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

  2. System.exit(0)和System.exit(1)区别

    System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() .无论如何,内存都释放了!也就是说连JV ...

  3. System.exit(0)和System.exit(1)区别:

    System.exit(0)是将你的整个虚拟机里的内容都停掉了,而finish()只是退出了activity,并没有退出应用,Application还是存在于内存中的,除非被系统回收.无论如何,内存都 ...

  4. System.exit(0)和System.exit(1)区别(转)

    转:http://www.cnblogs.com/xwdreamer/archive/2011/01/07/2297045.html 1.参考文献 http://hi.baidu.com/accpzh ...

  5. JAVA System.exit(0) 和 System.exit(1) 的区别

    System.exit(int state) 方法都是来结束当前运行的java虚拟机.所有System.exit(1).System.exit(0) 执行后都会退出程序. state为0时时正常退出, ...

  6. Environment.Exit(0) 、Application.Exit() 、this.Close() 、this.Dispose()的区别

    Application.Exit:通知winform消息循环退出.程序会等待所有的前台线程终止后才能真正退出.是一种强行退出方式,就像 Win32 的 PostQuitMessage().它意味着放弃 ...

  7. System.exit(0)作用

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

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

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

  9. exit(0)、exit(1)、exit(-1)的区别

    exit(0) - 正常退出 exit(1) - 异常退出(除0外,其他值均为异常退出)

随机推荐

  1. oc语言--BLOCK和协议

    一.BOLCK (一)简介 BLOCK是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. BOLCK和函数的相似性:(1)可以保存代码(2 ...

  2. CSS之Hack

    一.类内部Hack IE都能识别*;标准浏览器(如FF)不能识别*:IE6能识别*,但不能识别 !important, IE7能识别*,也能识别!important; FF不能识别*,但能识别!imp ...

  3. Silverlight 结合ArcGis

    原文 http://www.dotblogs.com.tw/justforgood/archive/2012/05/09/72064.aspx 终于有机会谈到ArcGis(觉得很冷门) ,不过既然都做 ...

  4. View not attached to window manager crash 的解决办法

    View not attached to window manager crash 的解决办法 转自:http://stackoverflow.com/questions/22924825/view- ...

  5. Implement Queue using Stacks 解答

    Question Implement the following operations of a queue using stacks. push(x) -- Push element x to th ...

  6. python连续爬取多个网页的图片分别保存到不同的文件夹

      python连续爬取多个网页的图片分别保存到不同的文件夹 作者:vpoet mail:vpoet_sir@163.com #coding:utf-8 import urllib import ur ...

  7. javac命令详解(上)

    摘自http://blog.csdn.net/hudashi/article/details/7058998   javac命令详解(上)                             ja ...

  8. Beauty of Array(模拟)

    M - M Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status P ...

  9. hpux操作系统的关机与重新启动命令

    关机  shutdown -hy 0 重新启动: shutdown -ry 0

  10. 检查ORACLE的警告文件的脚本

    检查两天内的须要重视的信息: vi   alter_error.sh echo "Check Alter Error:" cat $TRACE/alert_$ORACLE_SID. ...