About the execution sequence of finally and return (to be supplemented), finallyreturn
Code 1:
public abstract class Test { public static void main(String[] args) { System.out.println(beforeFinally()); } public static int beforeFinally(){ int a = 0; try{ a = 1; return a; }finally{ a = 2; } }}/**output:1*/
From the result, it seems that the statement in 'Finally 'is executed after 'Return'. Actually, the statement in 'Finally 'is actually executed before 'Return. So the question is, since it was executed before, why is the value of 'A' not overwritten?
The actual process is as follows: when the program executes the return method in the try {} statement, it will do this and store the returned results in a temporary stack, then the program will not return immediately, but will execute the program in finally {}. When executing 'a = 2', the program will only overwrite the value of, but it does not update the value to be returned in the temporary stack. After the execution, the main program will be notified that "the finally program has been executed and can be requested to return". At this time, the value in the temporary stack will be retrieved and returned. It should be clear that the value to be returned is saved to the temporary stack.
Zookeeper ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Code 2:
public abstract class Test { public static void main(String[] args) { System.out.println(beforeFinally()); } public static int beforeFinally(){ int a = 0; try{ a = 1; return a; }finally{ a = 2; return a; } }}/**output:2*/
Here, there is also a return in finally {}, so when this return is executed, the value in the temporary stack will be updated. Similarly, after finally execution, the main program will be notified to return the request, which is about to return the value in the temporary stack. Therefore, the returned value is 2.
Zookeeper ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Code 3:
Public class Test {public int add (int a, int B) {try {return a + B;} catch (Exception e) {System. out. println ("catch statement block");} finally {System. out. println ("finally statement block");} return 0;} public static void main (String argv []) {Test test = new Test (); System. out. println ("And yes:" + test. add (9, 34) ;}}/** output: finally statement block and yes: 43 */
Zookeeper ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Summary:
1. The finally {} code block is executed before return. 2. Multiple return statements are executed in sequence. After multiple return statements are executed, the subsequent return statements will not be executed. 3. Remember that, no matter whether an exception is thrown, finally will execute before return.