【问题记录】【JDK】关于 finally 中对返回结果的影响

1  前言

今儿复习 Java 的一些基本知识,发现关于finally 中对返回结果的影响,有点记不清了,这里再回忆一下。

2  分析

先说结论,Java的try语句在返回前将其他所有的操作执行完,保留好要返回的值,而后转入执行finally中的语句,而后分为以下三种情况:

(1)如果finally中有return语句,则会将try中的return语句”覆盖“掉,直接执行finally中的return语句。
(2)如果finally中没有return语句,也没有改变要返回值,则执行完finally中的语句后接着执行try中的return语句。
(3)如果finally中没有return语句,但是改变了要返回的值,分以下两种情况:

  • 如果return的是基本数据类型或引用类型,则在finally中对值或引用的改变不起作用,try中的return语句依然会返回之前保留的值。
  • 如果return的是引用类型,而在finally中修改该引用类型的属性值,try中return语句返回的对象,属性值是finally中改变后的属性值。

     

接下来我们看看示例:

public class FinallyReturnTest {
    public static void main(String[] args) {
        try {
            System.out.println(finallyReturnBasic());     // 2
            System.out.println(finallyReturnString());    // Change me!
            System.out.println(finallyReturnReference()); // innerB

            System.out.println(finallyChangeBasic());     // 1
            System.out.println(finallyChangeString());    // Hello World!
            System.out.println(finallyChangeReference()); // innerA
            System.out.println(finallyChangeAttribute()); // innerB
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static int finallyReturnBasic() throws Exception {
        int input = 1;

        try {
            return input;
        } catch (Exception e) {
            throw new Exception("test exception");
        } finally {
            return 2;
        }
    }

    public static String finallyReturnString() throws Exception {
        String input = "Hello World!";

        try {
            return input;
        } catch (Exception e) {
            throw new Exception("test exception");
        } finally {
            return "Change me!";
        }
    }

    public static Inner finallyReturnReference() throws Exception {
        Inner inner = new Inner("innerA");
        try {
            return inner;
        } catch (Exception e) {
            throw new Exception("test exception");
        } finally {
            return new Inner("innerB");
        }
    }

    public static int finallyChangeBasic() throws Exception {
        int input = 1;

        try {
            return input;
        } catch (Exception e) {
            throw new Exception("test exception");
        } finally {
            input = 2;
        }
    }

    public static String finallyChangeString() throws Exception {
        String input = "Hello World!";

        try {
            return input;
        } catch (Exception e) {
            throw new Exception("test exception");
        } finally {
            input = "Change u!";
        }
    }

    public static Inner finallyChangeReference() throws Exception {
        Inner inner = new Inner("innerA");
        try {
            return inner;
        } catch (Exception e) {
            throw new Exception("test exception");
        } finally {
            inner = new Inner("innerB");
        }
    }

    public static Inner finallyChangeAttribute() throws Exception {
        Inner inner = new Inner("innerA");
        try {
            return inner;
        } catch (Exception e) {
            throw new Exception("test exception");
        } finally {
            inner.setName("innerB");
        }
    }
}

class Inner {
    private String name;

    public Inner(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

3  小结

好啦,就看到这里,有理解不对的地方欢迎指正哈。

posted @ 2024-02-19 06:52  酷酷-  阅读(26)  评论(0编辑  收藏  举报