Java语法:finally中修改返回结果

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

  • 如果finally中有return语句,则会将try中的return语句”覆盖“掉,直接执行finally中的return语句。
  • 如果finally中没有return语句,也没有改变要返回值,则执行完finally中的语句后接着执行try中的return语句。
  • 如果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;
    }
}

你可能感兴趣的:(Java,java,开发语言)