查看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.     * 

* This method calls the exit method in class * Runtime. This method never returns normally. *

* The call System.exit(n) is effectively equivalent to * the call: *

     * Runtime.getRuntime().exit(n)     * 
* * @param status exit status. * @throws SecurityException * if a security manager exists and its checkExit * 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)是回到最上层

示例

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




更多相关文章

  1. Android应用程序退出实例
  2. Android安装应用程序出现“INSTALL_FAILED_CONTAINER_ERROR”错
  3. android 分享文字到其他应用程序
  4. Android:程序跳过登录界面直接进入主界面(自动登录)
  5. Android 指南针程序
  6. Android 截图程序实现 需要root权限

随机推荐

  1. Android(安卓)监听home键(android:launchM
  2. Android超链接去下划线--Android学习笔记
  3. Android(安卓)技术用于汇总
  4. Android(安卓)Edittext焦点处理;
  5. moodle手机版在android平台上的安装配置
  6. 在Eclipse中导入android sdk源码
  7. Android教程之Android(安卓)SDK1.5模拟器
  8. Android(安卓)Studio 更新问题
  9. 图解Android(安卓)- Android(安卓)GUI 系
  10. 安装Android时Could not find D:\Androi