java中finally與return的測試方法

直入正題,測試程序以下,看執行結果和你想的是否一致?函數

public class ReturnTest {
    public static void main(String[] args) {
        System.out.println("=============test1==================");
        System.out.println(test1());
        System.out.println("===============================");
        System.out.println("=============test1_1==================");
        System.out.println(test1_1());
        System.out.println("===============================");
        System.out.println("\n============test2===================");
        System.out.println(test2());
        System.out.println("===============================");
        System.out.println("\n============test2_1===================");
        System.out.println(test2_1());
        System.out.println("===============================");
        System.out.println("\n============test3===================");
        System.out.println(test3());
        System.out.println("===============================");
        System.out.println("\n============test3_1===================");
        System.out.println(test3_1());
        System.out.println("===============================");
    }

    public static String test1() {
        String a = "in try";
        try {
            return a;
        } catch (Exception e) {
        } finally {
            a = "in finally";
            System.out.println("do finally");
        }
        return a;
    }

    public static String test1_1() {
        String a = "in try";
        try {
            return a;
        } catch (Exception e) {
        } finally {
            a = "in finally";
            System.out.println("do finally");
            return a;
        }
    }

    public static int test2() {
        int a = 1;
        try {
            return a;
        } catch (Exception e) {
        } finally {
            a = 2;
            System.out.println("do finally");
        }
        return a;
    }

    public static int test2_1() {
        int a = 1;
        try {
            return a;
        } catch (Exception e) {
        } finally {
            a = 2;
            System.out.println("do finally");
            return a;
        }
    }

    public static Helper test3() {
        Helper a = new Helper();
        a.a = 1;
        try {
            return a;
        } catch (Exception e) {
        } finally {
            a.a = 2;
            System.out.println("do finally");
        }
        return a;
    }

    public static Helper test3_1() {
        Helper a = new Helper();
        a.a = 1;
        try {
            return a;
        } catch (Exception e) {
        } finally {
            a.a = 2;
            System.out.println("do finally");
            return a;
        }
    }

    static class Helper {
        int a;

        public String toString() {
            return String.valueOf(a);
        }
    }

}

結果以下:測試

=============test1==================
do finally
in try
===============================
=============test1_1==================
do finally
in finally
===============================

============test2===================
do finally
1
===============================

============test2_1===================
do finally
2
===============================

============test3===================
do finally
2
===============================

============test3_1===================
do finally
2
===============================

很好理解了。code

#結論:io

在try catch塊裏return的時候,finally也會被執行。class

return 語句會把後面的值複製到一份用來返回,若是return的是基本類型的,finally裏對變量的改動將不起效果,若是return 的是引用類型的,改動將能夠起效果。test

finally裏的return語句會把try catch塊裏的return語句效果給覆蓋掉。變量

看來return語句並不必定都是函數的出口,執行return時,只是把return後面的值複製了一份到返回值變量裏去了。看來最佳實踐是:引用

最好把return放到方法尾而不要在try cath 裏return程序

不要在try catch塊和finally塊裏都包含return方法

若是在try catch塊裏return, 則不要在finally塊裏操做被return的變量

相關文章
相關標籤/搜索