本文共 6353 字,大约阅读时间需要 21 分钟。
java java se
Oracle Corporation is going to release Java New Version: Java SE 9 around End of March 2017. So, I would like to deliver a series of Posts on Java SE 9 New Features. It’s my eighth post in this series.
Oracle Corporation将于2017年3月底发布Java新版本:Java SE9。因此,我想发表一系列有关Java SE 9新功能的文章。 这是本系列的第八篇。
In this post, we are going to discuss about some Improvements to Try-With-Resources in Java SE 9. Let us start exploring that construct now.
在本文中,我们将讨论Java SE 9中的Try-With-Resources的一些改进。现在让我们开始探索该构造。
Java SE 7 has introduced a new construct: Try-With-Resources Statement for better Exception Handling. Without this construct, Developer has to write lot of redundant and ugly code. If Developer forgets about closing resources properly, we will get Resource Leakage issues in our application.
Java SE 7引入了一种新的结构:Try-With-Resources语句,用于更好的异常处理。 没有这种构造,开发人员就不得不编写大量的冗余和丑陋的代码。 如果开发人员忘记正确关闭资源,我们将在应用程序中遇到资源泄漏问题。
The main goals of this new feature is :
此新功能的主要目标是:
Avoid writing some extract catch/finally block.
避免编写一些摘录捕获/最终块。
No need to check whether Resource reference is referring to an object or null.
无需检查资源引用是指对象还是null。
ARM (Automatic Resource Management)
ARM(自动资源管理)
Try-With-Resources Example-1:-
尝试使用资源示例1:-
void testARM_Before_Java9() throws IOException{ BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt")); try (BufferedReader reader2 = reader1) { System.out.println(reader2.readLine()); }}
Here we have created an utility method which creates a BufferedReader object to read the content of a file. If we observe the above code snippet, even though we have reader1 referring to BufferedReader object, we should create a duplicate one “reader2” to use it in Try-With-Resources. It is one small bug or issue in Java SE 7 or 8 versions.
在这里,我们创建了一个实用程序方法,该方法创建一个BufferedReader对象以读取文件的内容。 如果观察上述代码片段,即使我们的reader1引用了BufferedReader对象,我们也应该创建一个重复的“ reader2”以在Try-With-Resources中使用它。 它是Java SE 7或8版本中的一个小错误或问题。
We cannot use any Resource (which is declared outside the Try-With-Resources) within try() block of Try-With-Resources statement.
我们不能在Try-With-Resources语句的try()块内使用任何资源(在Try-With-Resources外部声明)。
Below code is invalid in Java SE 7 or 8 versions. It throws compile-time error.
以下代码在Java SE 7或8版本中无效。 它会引发编译时错误。
Try-With-Resources Example-2:-
尝试资源示例2:-
void testARM_Before_Java9() throws IOException{ BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt")); try (reader1) { System.out.println(reader1.readLine()); }}
NOTE:-
If you want to read more information about this component, please go through this tutorial:注意:-
如果您想阅读有关此组件的更多信息,请阅读本教程:In Java SE 7 or 8 versions, we should follow these rules to use Try-With-Resources statement for Automatic Resource Management(ARM)
在Java SE 7或8版本中,我们应遵循以下规则,以将Try-With-Resources语句用于自动资源管理(ARM)
Java SE 9 has provided some improvements to Try-With-Resources statement. As we discussed in previous sections, Java SE 7 or 8 versions have one small issue or bug with this statement.
Java SE 9对Try-With-Resources语句进行了一些改进。 如前几节所述,Java SE 7或8版本的此声明存在一个小问题或错误。
In Java SE 9, if we have a resource which is already declared outside the Try-With-Resource Statement as final or effectively final, then we do NOT need to declare a local variable. We can use previously created variable within Try-With-Resource Statement without any issues as shown below:
在Java SE 9中,如果我们已经在Try-With-Resource语句之外声明了资源为final或有效的final,那么我们就不需要声明局部变量。 我们可以在Try-With-Resource语句中使用先前创建的变量,而不会出现任何问题,如下所示:
Try-With-Resources Example-3:-
尝试资源示例3:-
void testARM_Java9() throws IOException{ BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt")); try (reader1) { System.out.println(reader1.readLine()); }}
This example is a valid code with Java SE 9. We do not create another local variable like reader2 to refer reader1 as shown in Example-1. Let us execute both examples in Java SE 9 REPL to test them.
该示例是Java SE 9的有效代码。我们没有创建另一个本地变量(如reader2)来引用Example1中所示的reader1。 让我们在Java SE 9 REPL中执行两个示例以对其进行测试。
Test Example-1 with Java SE 9 REPL:-
使用Java SE 9 REPL的测试示例1:-
jshell> void testARM_Before_Java9() throws IOException{ ...> BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt")); ...> try (BufferedReader reader2 = reader1) { ...> System.out.println(reader2.readLine()); ...> } ...> }| created method testARM_Before_Java9()jshell> testARM_Before_Java9()journaldev
Test Example-3 with Java SE 9 REPL:-
使用Java SE 9 REPL的测试示例3:-
jshell> void testARM_Java9() throws IOException{ ...> BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt")); ...> try (reader1) { ...> System.out.println(reader1.readLine()); ...> } ...> }| created method testARM_Java9()jshell> testARM_Java9()journaldev
That’s it all about “Java SE 9: Try-With-Resources Improvements” new feature. We will discuss some more Java SE 9 New Features in my coming posts.
这就是“ Java SE 9:尝试使用资源的改进”新功能的全部内容。 我们将在以后的文章中讨论更多Java SE 9新功能。
Please drop me a comment if you like my post or have any issues/suggestions/type errors.
如果您喜欢我的帖子或有任何问题/建议/类型错误,请给我评论。
Thank you for reading my tutorials.
感谢您阅读我的教程。
Happy Java SE 9 Learning!
Java SE 9学习愉快!
翻译自:
java java se
转载地址:http://yyqzd.baihongyu.com/