web
 

Java SE 9: Try-With-Resources Improvements


Post Brief Table of Content:

  • Introduction
  • Java SE 7: Try-With-Resources Basics
  • Java SE 7: Try-With-Resources Rules
  • Java SE 9: Try-With-Resources Improvements

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.

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 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.

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.

  • Better Resource Management
  • ARM (Automatic Resource Management)

  • To avoid memory leakages

Try-With-Resources Example-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.

We cannot use any Resource (which is declared outside the Try-With-Resources) within try() block of Try-With-Resources statement.

Below code is invalid in Java SE 7 or 8 versions. It throws compile-time error.

Try-With-Resources Example-2:-


void testARM_Before_Java9() throws IOException{
 BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt"));
 try (reader1) {
   System.out.println(reader1.readLine());
 }
}

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)

  • Any Resource (which Pre-defined Java API class or User Defined class) must implement 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)
  • That newly created local variable is valid to use within Try-With-Resources Statement.

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.

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:

Try-With-Resources Example-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.

Test Example-1 with Java SE 9 REPL:-


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:-


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.

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!






journaldev is optimized for learning.© journaldev .
All Right Reserved and you agree to have read and accepted our term and condition