java - 问题如图,为什么返回值是1
大家讲道理
大家讲道理 2017-04-18 09:56:11
0
7
318

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(7)
左手右手慢动作

http://m.blog.csdn.net/articl...

PHPzhong

As shown in the figure, first of all, you must understand that when
1, there is no return in finally: finally中没有return的时候:
会先执行try里面的,return会执行但是没有真正的return,此时去执行了finally里面的语句,然后再返回来执行return,所以如上题x1:1,然后return x++;这时候它会返回1出去,但是finally是无论如何都会执行的,所以它并不是立刻返回,而是先执行finally里的语句,然后得到 x3:3 will execute the contents of try first, and return will execute But there is no real return. At this time, the statement in finally is executed, and then returns to execute the return, so as in the above question x1:1, and then return x++; at this time it will return 1, but finally will be executed anyway, so it does not return immediately, but executes the statement in finally first, and then gets x3:3, but the key point is: because there is no return in finally, Therefore, the change to x in finally will not affect the value of the previous return, because the previous return was 1; so even if the value of x is changed in the subsequent finally, and then the return is executed again, the previous 1 will still be returned. That is to say, the return is the same when it goes out. It will not change unless it is finally returned again. 2. When there is return
in finally: will be executed first, and the return will be executed but there is no real return. At this time, execute the finally inside, then execute the return inside finally, and return directly.
Hope to adopt it.

阿神

Code flow
Declare variable x = 1x = 1
抓到异常 执行return x++ 此时x = 2,但是x++这个值是1,但是没有执行return这个动作
finally 执行, x = 3, 然后returnCatch the exception and execute return x++ At this time x = 2, but x++ code>This value is 1, but the return action is not executed

finally is executed, x = 3, and then returnJump back.🎜
伊谢尔伦

First throw an exception, and then return the x value (1) first, and then x in the class will increase by 1. I don’t know if it is right or not. I am also a beginner.

刘奇

return x++ operator, returns the left side first, then x increases by 1. return ++x should return 2

巴扎黑

The meaning of expression ++X is: X plus one, and the value of the expression is X plus one;

The meaning of expression X++ is: X plus one, but the value of the expression is still X.

小葫芦

1. Wherever there is a return in try catch finally, the current value is saved to the stack first, but it does not return immediately. The subsequent statements will still be executed, and the statements in finally will always be executed. However, the return value has been placed on the stack in advance, so the return value is still 1. If you put the return statements in try catch finally, you can see different execution results in sequence.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template