java.lang.System的 System.exit(int code)解析
2016-06-06 16:15
351 查看
/** * Terminates the currently running Java Virtual Machine. The * argument serves as a status code; by convention, a nonzero status * code indicates abnormal termination. * * Causes the VM to stop running and the program to exit with the given exit status. * If {@link #runFinalizersOnExit(boolean)} has been previously invoked with a * {@code true} argument, then all objects will be properly * garbage-collected and finalized first. * * @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) { untime.getRuntime().exit(status); } /** * Causes the VM to stop running and the program to exit with the given exit status. * If {@link #runFinalizersOnExit(boolean)} has been previously invoked with a * {@code true} argument, then all objects will be properly * garbage-collected and finalized first. */ public static void exit(int code) { Runtime.getRuntime().exit(code); }
注:这个方法是用来结束当前正在运行中的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是用来 表示这个程序是非正常退出。
相关文章推荐
- System.exit(0)和System.exit(1)区别
- 一行代码引发的“血案”
- System.exit(0)和System.exit(1)区别【转】
- Android System.exit(code) and android.os.Process.killProcess(pid)或者发生RunTimeException 导致应用重启
- System.exit(0)和System.exit(1)区别
- Android开发finish()和System.exit(0);的区别
- 求斐波那契数列前20项
- Java反射
- Face Recognizer
- Spring 事务管理高级应用难点剖析: 第 2 部分
- Java的jdbc使用addBatch进行批处理操作的几种方式
- Eclipse中的快捷键
- Spring 事务管理高级应用难点剖析: 第 1 部分
- Runtime.getRuntime.exec()的用法
- org.w3c.dom(java dom)解析XML文档
- Java JDBC 批处理插入数据操作
- 使用JAXB工具类实现标准rss到bean转换
- Java35:多线程
- 详解Java的MyBatis框架中SQL语句映射部分的编写
- JAVA 接口 Interface 学习笔记 implements