java java se_Java SE 9:尝试资源改进

java java se

发表简要目录: (Post Brief Table of Content:)

  • Introduction

    介绍
  • Java SE 7: Try-With-Resources Basics

    Java SE 7:试用资源基础
  • Java SE 7: Try-With-Resources Rules

    Java SE 7:试用资源规则
  • Java SE 9: Try-With-Resources Improvements

    Java SE 9:尝试资源改进

介绍 (Introduction)

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:试用资源基础 (Java SE 7: Try-With-Resources Basics)

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 :

此新功能的主要目标是:

  • Syntactic Sugar

    句法糖
  • Avoid writing some extract catch/finally block.

    避免编写一些摘录捕获/最终块。

  • Better Readable Code

    更好的可读代码
  • No need to do null checks

    无需做空检查
  • No need to check whether Resource reference is referring to an object or null.

    无需检查资源引用是指对象还是null。

  • Better Resource Management

    更好的资源管理
  • ARM (Automatic Resource Management)

    ARM(自动资源管理)

  • To avoid memory leakages

    为了避免内存泄漏

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: Java SE 7: Try-with-Resources

注意:-
如果您想阅读有关此组件的更多信息,请阅读本教程: Java SE 7:Try-with-Resources

Java SE 7:试用资源规则 (Java SE 7: Try-With-Resources Rules)

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)

  • Any Resource (which Pre-defined Java API class or User Defined class) must implement java.lang.AutoCloseable.

    任何资源(预定义Java API类或用户定义类)都必须实现java.lang.AutoCloseable。
  • Resource object must refer either final or effectively final variable

    资源对象必须引用最终变量或有效地最终变量
  • If Resource is already declared outside the Try-With-Resources Statement, we should re-refer with local variable (As shown in the above Example-1 code)

    如果已经在Try-With-Resources语句之外声明了Resource,则应使用局部变量重新引用(如上面的Example-1代码所示)
  • That newly created local variable is valid to use within Try-With-Resources Statement.

    该新创建的局部变量可以在Try-With-Resources语句中有效使用。

Java SE 9:尝试资源改进 (Java SE 9: Try-With-Resources Improvements)

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学习愉快!

翻译自: https://www.journaldev.com/12940/javase9-try-with-resources-improvements

java java se

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值