When---need finally:
Before jdk1.7, all related operations involving I/O, we will use finally to ensure that the flow in the last normal shutdown. After jdk1.7, although all streams that implement the Closable interface can be defined in a try block, the JVM automatically shuts down the input and output stream. But in fact, before we need to return to the code block, we implement an operation A that is performed regardless of whether the previous operation was successful or not. At this point we can put a into the finally block. One common operation is the unlock operation of the lock.
What---is finally:
The literal explanation is ultimately, eventually. The function is to ensure that the statements in the finally are executed after the execution of the code in the try block is complete. Regardless of whether an exception is thrown in the try block.
How---use finally:
Finally blocks must be used in conjunction with a try block and cannot be used alone.
Deeper---in-depth exploration of finally blocks:
Before delving into the study, we will give four conclusions directly:
1. Finally block executes only if the try block is executed
2. The finally block executes after the execution of the try block is completed, or the try block does not complete but next controls the transfer statement (Return/continue/break) before the control transfer statement.
3. The control transfer statement will present the return value in the local variable before the finally statement is executed
4. The control transfer return statement in a finally block can overwrite the return value in a try or catch block
which 1, 2 do not further explain. Let's take a closer look at 3 and 4 directly through two examples:
For an example of 3:
1 Public Static voidMain (string[] args) {2System.out.println ("return" +testfinally ());3 }4 5 Public StaticString testfinally () {6 inti = 10;7 Try {8 returni + "";9}finally {Teni++;//even if the value of I changes, but the value of I referred to in the return statement is saved in the local variable One } A } - - //the output is ten.
We analyze the byte code directly:
1 Public Staticjava.lang.String testfinally ();2Descriptor: () ljava/lang/String;3 Flags:acc_public, Acc_static4 Code:5Stack=3, locals=3, args_size=060:bipush 10--push 10 into the stack72: Istore_0--Store the top element (10) in the local variable 083:New#22//class Java/lang/stringbuilder96: DUPTen7: ILOAD_0--load data from local variable 0 (10) into the stack, subsequent code is used for string concatenation One8:invokestatic #47//Method java/lang/string.valueof: (I) ljava/lang/string; A11:invokespecial #26//Method Java/lang/stringbuilder. " <init> ":(ljava/lang/string;) V -14:invokevirtual #37//Method java/lang/stringbuilder.tostring: () ljava/lang/string; -17: astore_2--the result of stitching is stored in local variable 2 ("" ") the18:iinc 0, 1--value of local variable 0 plus 1 -21st: aload_2--load data from local variable 2 ("" ") -22: Areturn--return results -23: astore_1--Deposit The result of the previous variable 0 plus 1 into the local variable 1 +24:iinc 0, 1--local variable 0 plus 1 -27: aload_1--load the value of local variable 1 +28:athrow
In fact, Areturn returns the reference value of the variable. This means that if the object type is ultimately returned. Then, before the finally block executes, only the value of the object reference is stored in the local variable, not the deep copy of the object. We can change the properties of the returned object in the finally block. Just take a look at the next example:
1 Public Static voidMain (string[] args) {2System.out.println ("return" +testfinally ());3 }4 5 Public StaticMap<string, string>testfinally () {6map<string, string> map =NewHashmap<string, string>();7 Try {8Map.put ("AAA", "AAA");9 returnmap;Ten}finally { OneMap.put ("AAA", "BBB"); AMap =NULL; - } - } the - //output is: return {AAA=BBB}
Now let's talk about the 4th example:
1 Public Static voidMain (string[] args) {2System.out.println ("return" +testfinally ());3 }4 5 Private StaticString testfinally () {6 Try {7 return"456";8}finally {9 return"123";Ten } One } A - //output: 123
1 Public Static voidMain (string[] args) {2System.out.println ("return" +testfinally ());3 }4 5 Private StaticString testfinally () {6 Try {7 Throw NewSQLException ();8 //return "456";9}Catch(SQLException e) {TenSystem.out.println ("Catch sqlexception!"); One Throw NewRuntimeException ("123"); A } - finally { - return"123"; the } - } - - //the output is: + Catchsqlexception! - return123
More Other examples:
1 Public Static intGetValue () {2 inti = 1;3 Try {4i = 4;5}finally {6i++;7 returni;8 }9 }Ten //Final return 5 One A Public Static intGetValue () { - inti = 1; - Try { thei = 4; -}finally { -i++; - } + - returni; + } A //Final return 5 at - Public Static voidMain (string[] args) { - System.out.println (Test ()); - } - Public StaticString Test () { - Try { inSystem.out.println ("Try block"); - returntest1 (); to}finally { +System.out.println ("Finally block")); - } the } * Public StaticString test1 () { $SYSTEM.OUT.PRINTLN ("return statement");Panax Notoginseng return"After Return"; - } the //Final output: + TryBlock A returnStatement the finallyBlock +Afterreturn
Reference Links:
https://www.ibm.com/developerworks/cn/java/j-lo-finally/
Http://www.cnblogs.com/lanxuezaipiao/p/3440471.html
http://blog.csdn.net/michealmc/article/details/52237639
Java finally in-depth exploration