In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use finally grammar in Java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
I. finally sentence block
1. Note:
(1) finally statement block can be used in conjunction with try statement block directly. Try...finally....
(2) try.catch.finally can also be executed
(3) the code in the finally statement block is bound to be executed.
Package com.bjpowernode.java_learning;import java.io.*;public class D64room1 _ {public static void main (String [] args) throws FileNotFoundException {try {System.out.println ("ABC");} finally {System.out.println ("jdfoaij");} try {/ / System.exit (0); / / this sentence represents the exit of Java virtual machine FileInputStream F1 = new FileInputStream ("jfdioa") / / the following statement will not execute System.out.println ("ABC");} finally {/ / regardless of whether the statement in try is incorrect or not, the following statement will execute System.out.println ("jfaio");}
two。 Go deep into finally statement blocks
Public class D64room1 _ {public static void main (String [] args) throws FileNotFoundException {int I = M1 (); System.out.println (I);} public static int M1 () {int I = 10; try {return I;} finally {iride; System.out.println ("I of M1 =" + I) / / 11} / * * the execution principle of the above code, * int I = 10; * trt {* int temp = I; * return temp; *} finally {* icodes; * System.out.println ("M1 I =" + I); / / 11 *} * /}}
The 3.fianlly statement block must be executed, so usually in the program, in order to ensure that a resource must be released, resources are usually released in the finally statement block.
Package com.bjpowernode.java_learning;import java.io.*;public class D64room1 _ {public static void main (String [] args) throws FileNotFoundException {FileInputStream f2 = null; try {f2 = new FileInputStream ("jfoda");} catch (FileNotFoundException e) {e.printStackTrace ();} finally {if (f2! = null) {try {f2.close () } catch (IOException e) {e.printStackTrace ();}
II. The difference between final, finalize and finally
Final is a Java keyword that modifies variables that cannot be repeated before being assigned; member functions that are modified by it cannot be overridden again.
Finalize () is a method in Object that is automatically called before garbage collection.
Finally is a syntactic structure of exception handling mechanism.
In fact, the three are not related to each other, they are different grammars.
Thank you for reading this article carefully. I hope the article "how to use finally Grammar in Java" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.