Java's strange behavior while returning from f

2019-02-12 16:47发布

Try this piece of code. Why does getValueB() return 1 instead of 2? After all, the increment() function is getting called twice.

    public class ReturningFromFinally
    {
      public static int getValueA() // This returns 2 as expected
      {
         try     { return 1; }
         finally { return 2; }
      }

      public static int getValueB() // I expect this to return 2, but it returns 1
      {
        try     { return increment(); }
        finally { increment(); }
      }

      static int counter = 0;

      static int increment()
       {
          counter ++;
          return counter;
       }

      public static void main(String[] args)
      {
          System.out.println(getValueA()); // prints 2 as expected
          System.out.println(getValueB()); // why does it print 1?
      }
}

6条回答
forever°为你锁心
2楼-- · 2019-02-12 16:55

After all, the increment() function is getting called twice.

Yes, but the return value is determined before the second call.

The value returned is determined by the evaluation of the expression in the return statement at that point in time - not "just before execution leaves the method".

From section 14.17 of the JLS:

A return statement with an Expression attempts to transfer control to the invoker of the method that contains it; the value of the Expression becomes the value of the method invocation. More precisely, execution of such a return statement first evaluates the Expression. If the evaluation of the Expression completes abruptly for some reason, then the return statement completes abruptly for that reason. If evaluation of the Expression completes normally, producing a value V, then the return statement completes abruptly, the reason being a return with value V.

Execution is then transferred to the finally block, as per section 14.20.2 of the JLS. That doesn't re-evaluate the expression in the return statement though.

If your finally block were:

finally { return increment(); }

then that new return value would be the ultimate result of the method (as per section 14.20.2) - but you're not doing that.

查看更多
放荡不羁爱自由
3楼-- · 2019-02-12 16:59

The purpose of the finally method is to ensure that ressources are closed in any case. Consider the example:

public List<Person> getPersons() {
    Connection conn = openConnection();
    try {
        return selectPersons(conn);
    } finally {
        conn.close()
    }
}

The conn.close() statement is executed AFTER selectPersons(conn) is executed. Otherwise selectPersons(conn) would raise an connection closed error.

查看更多
迷人小祖宗
4楼-- · 2019-02-12 17:01

Because in you're getValue2() method, you're finally block just calls increment(), it does not return it. So what your code is doing is it increments and returns counter (1), and then increments counter to 2, but doesn't return it.

查看更多
混吃等死
5楼-- · 2019-02-12 17:02

the finally block in GetValue2 method does not return anything. It's only calling the method to increment counter.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-02-12 17:02

You do not have an explicit return in the second example. In this case, it will return the value within the try block. It makes intuitive sense because Java has already executed the code inside the try block. It would not the executed that block again after executing the finally block.

查看更多
老娘就宠你
7楼-- · 2019-02-12 17:07

See my comment.

It would return 2 if you had finally { return increment(); }. The first return statement's expression is evaluated prior to the finally block. See Section §14.20.2 of the JLS.

If execution of the try block completes normally, then the finally block is executed, and then there is a choice:

  • If the finally block completes normally, then the try statement completes normally.
  • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S.

Calling getValue2 (as you have it now) twice would result in 1 followed by 3.

查看更多
登录 后发表回答