为什么在 finally 块中更改返回的变量不会更改返回值? [英] Why does changing the returned variable in a finally block not change the return value?

查看:24
本文介绍了为什么在 finally 块中更改返回的变量不会更改返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Java 类,如下所示:

I have a simple Java class as shown below:

public class Test {

    private String s;

    public String foo() {
        try {
            s = "dev";
            return s;
        } 
        finally {
            s = "override variable s";
            System.out.println("Entry in finally Block");  
        }
    }

    public static void main(String[] xyz) {
        Test obj = new Test();
        System.out.println(obj.foo());
    }
}

这段代码的输出是这样的:

And the output of this code is this:

Entry in finally Block
dev  

为什么 s 没有在 finally 块中被覆盖,但控制打印输出?

Why is s not overridden in the finally block, yet control printed output?

推荐答案

try 块以 return 语句和 s 的值的执行完成return 语句执行时的 是方法返回的值.finally 子句后来更改了 s 的值(在 return 语句完成之后)这一事实并没有(此时)更改 return价值.

The try block completes with the execution of the return statement and the value of s at the time the return statement executes is the value returned by the method. The fact that the finally clause later changes the value of s (after the return statement completes) does not (at that point) change the return value.

请注意,以上处理的是对 finally 块中 s 本身的值的更改,而不是对 s 引用的对象的更改.如果 s 是对可变对象的引用(String 不是)并且对象的 contents 中最终被更改 块,那么这些更改将在返回值中看到.

Note that the above deals with changes to the value of s itself in the finally block, not to the object that s references. If s was a reference to a mutable object (which String is not) and the contents of the object were changed in the finally block, then those changes would be seen in the returned value.

所有这些操作的详细规则可以在 Java 语言规范的第 14.20.2 节.请注意,return 语句的执行算作 try 块的突然终止(开始的部分如果 try 块的执行对于任何其他原因 R...."适用).请参阅JLS 第 14.17 节 为什么 return 语句是块的突然终止.

The detailed rules for how all this operates can be found in Section 14.20.2 of the Java Language Specification. Note that execution of a return statement counts as an abrupt termination of the try block (the section starting "If execution of the try block completes abruptly for any other reason R...." applies). See Section 14.17 of the JLS for why a return statement is an abrupt termination of a block.

通过进一步的细节:如果 try 块和 finallytry-finally 语句由于 return 语句而突然终止,则 §14.20.2 中的以下规则适用:

By way of further detail: if both the try block and the finally block of a try-finally statement terminate abruptly because of return statements, then the following rules from §14.20.2 apply:

如果 try 块的执行由于任何其他原因 R [除了抛出异常] 突然完成,则 finally 块被执行,然后有一个选择:

If execution of the try block completes abruptly for any other reason R [besides throwing an exception], then the finally block is executed, and then there is a choice:

  • 如果 finally 块正常完成,则 try 语句由于 R 原因突然完成.
  • 如果 finally 块因 S 原因突然完成,则 try 语句因 S 原因突然完成(并且 R 被丢弃).
  • If the finally block completes normally, then the try statement completes abruptly for reason R.
  • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).

结果是finally块中的return语句决定了整个try-finally语句的返回值,返回的try 块中的值被丢弃.类似的事情发生在 try-catch-finally 语句中,如果 try 块抛出异常,它被 catch 块捕获,并且catch 块和 finally 块都有 return 语句.

The result is that the return statement in the finally block determines the return value of the entire try-finally statement, and the returned value from the try block is discarded. A similar thing occurs in a try-catch-finally statement if the try block throws an exception, it is caught by a catch block, and both the catch block and the finally block have return statements.

这篇关于为什么在 finally 块中更改返回的变量不会更改返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆