标签:java final finally finalize
Java编程中,经常会使用到异常处理,而finally看似的是try/catch后对逻辑处理的完善,其实里面却存在很多隐晦的陷阱;final常见于变量修饰,那么你在内部类中也见过吧;finalize作为GC回收对象前的一道门,什么时候执行,执行效果又是怎样,有时看看又忘了。下面是我总结网上朋友的一些博文及其帖子对三者进行总结。(重点讲下finally)
先看final
package java_zhaohuaxishi; public class Example1 { public int test(){ int i = 0; try{ i = 10; System.out.println("try"); return i+=10; }catch(Exception e){ i = 20; return 200; }finally{ System.out.println("finally"); System.out.println(i); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(new Example1().test()); } }运行结果:
try finally 20 20这也意味着finally打印出来的是try中return已经计算了的,验证观点一。
package java_zhaohuaxishi; public class Example1 { public int test(){ int i = 0; try{ i = 10; return 100; }catch(Exception e){ i = 20; return 200; }finally{ return i; } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(new Example1().test()); } }上述时没有出现异常情况,打印如下:
10
package java_zhaohuaxishi; public class Example2 { public int test(){ int i = 0; try{ i = 10/0; return 100; }catch(Exception e){ i = 20; return 200; }finally{ return i; } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(new Example2().test()); } }打印结果如下:
20结果是:无论出现异常与否,finally的return总会覆盖try/catch中return的结果,验证观点二。
package java_zhaohuaxishi; public class Example5 { public int test(){ int i = 0; try{ i = 10; return i; }catch(Exception e){ i = 20; return i; }finally{ i=30; //return i; } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(new Example5().test()); } }打印结果如下:
10可以看到,实际上finally修改i变量是不起作用的,验证观点三。
package java_zhaohuaxishi; class Test{ int num; public Test(int num){ this.num = num; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } public class Example3 { public Test test(){ Test t = new Test(0); try{ t.setNum(10); return t; }catch(Exception e){ t.setNum(20); return t; }finally{ t.setNum(30); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(new Example3().test().getNum()); } }打印结果如下:
30从上述结果来看,finally操作的对象确实是与try上的同一个对象;那么我们比较上面观点三,操作变量的时候是不能更改的,想想有点诡异,我们看下面代码:
package java_zhaohuaxishi; class Test{ int num; public Test(int num){ this.num = num; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } public class Example3 { public int test(){ Test t = new Test(0); try{ t.setNum(10); System.out.println(t.getNum()); return t.getNum(); }catch(Exception e){ t.setNum(20); return t.getNum(); }finally{ System.out.println(t.getNum()); t.setNum(30); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(new Example3().test()); } }这次我们不在返回对象,而是返回一个int变量。从观点一我们可以知道,返回的t.getNum()实际上是会先计算再保存起来。那么如果我们在finally中在去改变t的num,实际上t的num会被改变,然而返回的应该还是10!
10 30 10果然!这与我们预先的是一模一样。如果有人觉得finally中t对象可能与try中不一致,下面例子将会让你觉得很神奇:
package java_zhaohuaxishi; class Test4{ int num; public Test4(int num){ this.num = num; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } public class Example4 { public Test4 test(Test4 t){ System.out.println("传进去的t:"+t.hashCode()); try{ t.setNum(10); return t; }catch(Exception e){ t.setNum(20); return t; }finally{ //t.setNum(30); System.out.println("finally修改前:"+t.hashCode()); t = new Test4(0); System.out.println("finally修改后:"+t.hashCode()); //return t; } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Test4 t = new Test4(0); System.out.println("return返回对象"+new Example4().test(t).hashCode()); System.out.println("最后的t:"+t.hashCode()); } }
传进去的t:2004703190 finally修改前:2004703190 finally修改后:1175576547 return返回对象2004703190 最后的t:2004703190这结果看起来很神奇。我们验证观点四的时候操作对象是起作用的!然而当我们试图去修改t引用让他指向其他对象的时候竟然无效......
版权声明:本文为博主原创文章,转载请注明出处。
朝花夕拾——finally/final/finalize拨云雾见青天
标签:java final finally finalize
原文地址:http://blog.csdn.net/u010794180/article/details/47128003