关于return和finally

1.如果返回的是个对象,finally里的的代码,可以改变对象内部的状态。
Java代码   收藏代码
  1. package com.chinaso.phl;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String[] args) throws Exception {  
  9.         Test t = new Test();  
  10.         System.out.println(t.check());  
  11.     }  
  12.   
  13.     public List<String> check() throws Exception {  
  14.         List<String> name = new ArrayList<String>();  
  15.         name.add("phl");  
  16.         try {  
  17.             System.out.println("try");  
  18.             return name;  
  19.         } catch (Exception e) {  
  20.             e.printStackTrace();  
  21.         } finally {  
  22.             System.out.println("finally");  
  23.             name.add("piaohailin");  
  24.             for (int i = 0; i < 3; i++) {  
  25.                 System.out.println("time:" + (i + 1) * 300);  
  26.                 Thread.sleep(300);  
  27.             }  
  28.         }  
  29.         System.out.println("return");  
  30.         name.add("return");  
  31.         return name;  
  32.     }  
  33. }  

输出
try
finally
time:300
time:600
time:900
[phl, piaohailin]

2.finally里面的赋值,不会影响返回结果
Java代码   收藏代码
  1. package com.chinaso.phl;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String[] args) throws Exception {  
  9.         Test t = new Test();  
  10.         System.out.println(t.check());  
  11.     }  
  12.   
  13.     public String check() throws Exception {  
  14.         String name = "phl";  
  15.         try {  
  16.             System.out.println("try");  
  17.             return name;  
  18.         } catch (Exception e) {  
  19.             e.printStackTrace();  
  20.         } finally {  
  21.             System.out.println("finally");  
  22.             name = "piaohailin";  
  23.             for (int i = 0; i < 3; i++) {  
  24.                 System.out.println("time:" + (i + 1) * 300);  
  25.                 Thread.sleep(300);  
  26.             }  
  27.         }  
  28.         System.out.println("return");  
  29.         name = "return";  
  30.         return name;  
  31.     }  
  32. }  

输出
try
finally
time:300
time:600
time:900
phl

3.finally里带有return方法,则此return会覆盖try里面的return
//warnning finally block does not complete normally
一般不这么用,因为有警告信息,不够优雅
Java代码   收藏代码
  1. package com.chinaso.phl;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String[] args) throws Exception {  
  9.         Test t = new Test();  
  10.         System.out.println(t.check2());  
  11.     }  
  12.   
  13.     public String check2() throws Exception {  
  14.         String name = "phl";  
  15.         try {  
  16.             System.out.println("try");  
  17.             return name;  
  18.         } catch (Exception e) {  
  19.             e.printStackTrace();  
  20.         } finally {  
  21.             System.out.println("finally");  
  22.             name = "piaohailin";  
  23.             for (int i = 0; i < 3; i++) {  
  24.                 System.out.println("time:" + (i + 1) * 300);  
  25.                 Thread.sleep(300);  
  26.             }  
  27.             return name;  
  28.         }  
  29.     }  
  30. }  

输出
try
finally
time:300
time:600
time:900
piaohailin

猜你喜欢

转载自blog.csdn.net/u010074988/article/details/80336685