Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What syntax has been enhanced by the java9 version

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "what syntax has been enhanced in the java9 version?" interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what grammar has been enhanced by the java9 version.

First, let's talk about Java7's try-with-resources.

Before Java 7, there was no try-with-resources syntax, and all the stream destruction actions had to be manually written in the finally method to close. The following code writes a string to a file.

@ Testvoid testStream () throws IOException {String fileName = "D:\ data\\ test\\ testStream.txt"; FileOutputStream fos = new FileOutputStream (fileName); / / create IO pipeline flow OutputStreamWriter osw = new OutputStreamWriter (fos); BufferedWriter bw = new BufferedWriter (osw); try {bw.write ("handwritten code to close Stream flow"); bw.flush ();} finally {bw.close (); / / manually close IO pipeline flow osw.close () Fos.close ();}}

The try-with-resources syntax has been provided since the Java 7 version. We just need to include the pipeline flow with try (). After the execution of the try code snippet, the IO pipeline flow will be closed automatically. There is no need for us to write code to close it. This is very simple!

@ Testvoid testTry () throws IOException {String fileName = "D:\ data\\ test\\ testTry.txt"; try (FileOutputStream fos = new FileOutputStream (fileName); OutputStreamWriter osw = new OutputStreamWriter (fos); BufferedWriter bw = new BufferedWriter (osw); {bw.write ("IO pipeline flow is automatically called close () method"); bw.flush ();}} II. Avoid misunderstanding.

After many friends know the try-with-resources grammar, they are easy to get into misunderstandings.

Myth one: only IO pipeline flow can use try-with-resources syntax for automatic resource closure. Second, all class objects with the close () method will automatically call the close () method to close the resource.

Misunderstanding one narrows the scope of practice, while misunderstanding two exaggerates the scope of practice. So what kind of resources can be turned off automatically? The answer is that classes that implement the AutoCloseable or Closeable interface can automatically shut down resources.

Public interface Closeable extends AutoCloseable {public void close () throws IOException;}

The Closeable interface inherits from the AutoCloseable interface, and both contain the close () method. If your custom class that takes up system resources needs to be recycled, implement one of these two interfaces and do resource recovery and shutdown in the close () method. In this way, your custom classes can also be recycled and closed using try-with-resources syntax.

Third, the improvement of try-with-resources in Java 9

The try-with-resources syntax has been improved in java 9. The try () of the try-with-resources syntax can contain variables, separated by semicolons. The purpose of this improvement is to make the semantics clearer and separate the resource creation code from the syntax of trying to recycle resources.

Semantics one: try to execute the code snippet, if an exception is thrown, handle the exception semantics two: try to automatically close the resource, close who? Close @ Testvoid testJava9Try () throws IOException {String fileName = "D:\\ data\\ test\\ testJava9Try.txt"; FileOutputStream fos = new FileOutputStream (fileName); OutputStreamWriter osw = new OutputStreamWriter (fos); BufferedWriter bw = new BufferedWriter (osw); try (bw;osw) Fos) {/ / Note here: try to recycle the corresponding resources of these three objects, and compare bw.write ("Java9- can be automatically called close () method") with the java 7 code above; bw.flush ();}} so far, I believe you have a deeper understanding of "what syntax has been enhanced by the java9 version". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report