java try{} finally{}

 1 public class FinallyTest { 
 2     public static void main(String[] args) { 
 3     System.out.println(new FinallyTest().test());//这个打印结果为1. 
 4     System.out.println(new FinallyTest().get());// 这个打印结果为2. 
 5     System.out.println(new FinallyTest().testa().a);// 这个打印结果为2.
 6     } 
 7 
 8     //调这个就打印1 
 9     static int test() { 
10     int x = 1; 
11     try { 
12     return x; 
13     } finally { 
14      ++x;
15     } 
16     }
17     
18     
19     static A testa() { 
20         A x = new A();
21         try { 
22             x.a = "123";
23         return x; 
24         } finally { 
25         x.a="234";
26         x=null;
27         System.out.println(x);
28         } 
29         }
30     //调这个就打印2 
31     static public int get() { 
32     try { 
33         
34     return 1; 
35     } catch(Exception e){
36         return 3;
37     } finally { 
38     return 2; 
39     } 
40     } 
41     }
42 
43 class A{
44     
45     public String a;
46 }
View Code

结论:finnaly不影响返回结果的,在return 之前,return的值被储存在临时变量里,finally无论做如何操作都不影响返回的值,除非在finnaly又返回了另外的值

posted @ 2014-04-25 12:47  竹子院  阅读(201)  评论(0)    收藏  举报