Java, and finally return the execution order of Detailed

java method is performed in the stack frame, the stack frame is a thread private stack unit, a thread of execution method for a small stack space will be allocated to each of the memory space as a method of performing the method, the stack frame is divided into three regions :

1. The operand stack is used to save the operands of the expression being executed, the data structure of the polynomial evaluator learned based on a stack algorithm, the operand stack and the role as

2. The local variables, save for the variable used in the method, the method including parameters, variables declared inside methods, member variables and member variables used in the method or the class object (static variables), last two variables copied to the local variables, so in a multithreaded environment, this variable needs to be declared as volatile types as needed

3. Bytecode instruction region, do not explain this, the method in the instruction code is translated into

return statement

implementation return statement without a finally block. Now let's take a look at the return statement return statement format is as follows:

    return [expression];

Wherein expression The (expression) is optional, because some method does not return a value, so there is no return back expression, or expression can be seen as empty.
  We know the role of a return statement may conclude the method and return a value, so his return is where the value of it? Returns the return instruction execution time, the value of the operand stack, no matter what kind of expression is an expression, what has it done the work, not important for the return instruction, he is only responsible for the operands top of the stack The value returned. The return expression is divided into two parts performed:

执行:expression;
执行:return指令;

For example: return x + y; this code to perform x + y, and then performing return; of x and y is first performed from the copy to the local variables of the operand stack instructions, then the addition is executed, this time the result x + y the value stored in the operand stack of the stack, and finally execute the return instruction, the return value of the operand stack.
  
For return x; x first implementation, x is an expression that has only one operand, executes the variable x is copied from the local variables to operand stack instructions, and return execution, the operand stack of return value. Thus return x; actual return when the return instruction is executed, the operand x in a stack snapshot or copy is called, rather than the value x.

finally block:

If you have finally block method, then the return statement is how to implement it? For example the following code:

try {
    return expression;
} finally {
    do some work;
}

First of all we know, finally statement will be executed, but their order of execution is kind of how it? Their order of execution is as follows:
  
1 execute:. Expression, the expression is evaluated, the result is stored in the operand stack;

2 perform: the top of stack operand (expression result) to the local variables as a return value;

3. execute: finally block code statement;

4. execution: copying the second step to the return value of the local variables and operand stack copy back;

5. execute: return instruction, the return value of the operand stack;

We can see that in the first step is finished, the return value of the overall process have been identified, but also due to the finally block is executed, the program will return the value temporarily stored in the local variables, the operand stack is used to make finally statement execution code block, and the like finally finished, and then temporarily stored and copied back to the return value of the operand stack. So no matter what operation to perform finally block, it can not affect the return value, so attempts to modify the finally block the return value is futile . So, finally statement block is designed just to get the method to perform some important finishing touches, and not used to calculate the return value.

Sample code:

public class FinallyDemo {
    public int testMethod(String _int,String _className){
        int x = 1;
        try{
            Integer.valueOf(_int);
            Class.forName(_className);
            //如果上面两句代码没有发生异常,对于return x这句代码,程序会先计算表达式x
            //即将x从局部变量区复制到操作数栈顶,结果就是操作数栈顶的值,也就是x的值,为1
            //然后将操作数栈顶的值复制到局部变量区(假设这个复制到局部变量区的值叫returnvalue),再执行finally代码块,在finally代码块
            //中,x的值被修改为3(即局部变量区中的x值),finally执行完,程序又将returnvalue复制到操作数栈顶,然后执行return指令,返回
            //操作数栈顶的值,最终返回值是1
            return x;
        }catch(ClassNotFoundException e){
            //同样发生了ClassNotFoundException,x的值被修改成2
            //因此在catch中的return x语句中定义了返回值大小为2,所以最终返回的是2
            x = 2;
            return x;
        }finally{
            //这句代码一定会执行的,这里的代码执行结束后才会结束方法并返回值,因此在finally语句修改x不会影响返回值,因为返回值在return 后面的
            //表达式执行完就已经确定了,他是x的快照,而不是x
            x = 3;
        }
    }
    public static void main(String [] args){
        try{
            FinallyDemo demo = new FinallyDemo();
            int i_1 = demo.testMethod("123123", "expert.in.java.lang.CalendarDemo");
            System.out.println(i_1);//结果为1
            int i_2 = demo.testMethod("123123", "com.edu.cn.qj.MyClass");
            System.out.println(i_2);//结果为2
            int i_3 = demo.testMethod("dsadf", "expert.in.java.lang.CalendarDemo");
            System.out.println(i_3);//没有返回值,会抛出异常
        }finally{

        }
    }
}

发布了457 篇原创文章 · 获赞 94 · 访问量 1万+

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104736962
Recommended