java-finally used

java-finally used

You must first know jvm stack

(1) .jvm stack method is used to push the stack operation, a thread stack frame, the stack is private jvm, jvm basic operation unit of the stack is a stack frame, the frame comprising a stack of three part, the local variable table, the operand stack, the program counter

Local variable table is used to store process parameters, local variables and internal methods defined operand stack is used for the calculation of specific values. The program counter is used to record the running position.

Armed with the knowledge of the top, which can finally understand why the return value can not be changed.

int a=10;

return a+1

finally  a+4

Can be seen in the program will first find the local variable table 10, a pointer to it, if not the variable is created. After that, the 10 copy of a local variable to the operand stack, operate a + 1, and at this time, when the normal procedure is to return, but finally keyword is a procedure that must be performed, so this time, will operand stack 11, copied to the local variable table, and then proceeds to a + 4 operand stack, after the calculation, the original 11 again performs return stack, the return value of the operand stack. So finally it is generally used for more important finishing work, rather than to calculate the return value, unless the return will be in finally return

Guess you like

Origin www.cnblogs.com/fan123yh/p/11712859.html