面试代码
来源:互联网 发布:新版淘宝怎么看足迹 编辑:程序博客网 时间:2024/05/29 20:00
public static void main(String[] args) {
int a= 10;
int b= 10;
method(a,b);
System.out.println("a:"+a);
System.out.println("b:"+b);
}
int a= 10;
int b= 10;
method(a,b);
System.out.println("a:"+a);
System.out.println("b:"+b);
}
写一个method方法,使main方法里面a打印出来的值为100,b为200
代码如下:
public static void method(int a,int b){ /**法1: * */// System.out.println("a="+a*a+",b="+2*b*b);// System.exit(0); /** * 法2: */ PrintStream Sys = new PrintStream(System.out){ public void println(String str){ if(str != null){ if(str.startsWith("a")){ super.println("a="+100); } if(str.startsWith("b")){ super.println("b="+200); } }else{ throw new NullPointerException(); } } }; System.setOut(Sys); }}查看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) { untime.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是用来表示这个程序是非正常退出。
而方法二是将println方法进行了从写
0 0
- 面试代码
- 面试代码
- 面试代码
- 面试->代码
- 面试集锦代码部分
- Java 面试代码题型
- 【leetcode】面试leetcode代码
- js面试代码题
- 面试手写代码题目
- 前端面试(代码)
- php面试(代码)
- 常见面试代码总结
- 面试代码题记录
- 面试宝典之代码编写
- 【面试】一些排序算法代码
- 快速排序 模板代码 面试
- 程序员面试,写代码要点
- 面试必会代码总结
- 如何在Linux中查找一个文件
- vue2.0 $refs的使用
- PAT 1046
- 1.4:被隐形的具体实现
- 算法研究:已知不重复的int集合,求最长递增子序列
- 面试代码
- CodeForces - 510C(拓扑排序)
- Snackbar
- 第11章 其他常用工具
- maven 安装jar包到本地仓库
- 关于偏置-方差分解
- 管道(pipe)和有名管道(fifo)
- C++下载文件到本地
- 第12章 处理信号