In this article, we are going to discuss about the improvement made in ‘Try-With-Resources’ feature in the Java 9 Release. But, in order to understand the change being made, we need to know first what actually ‘try-with-resources’ feature is. So, let’s go ahead and learn all of that in this post with examples.
Check out: Iterate / ofNullable operations in Java 9 Stream
Try-With-Resources Improvement in Java 9...!!! Click To Tweet
In this article, we are going to cover below points:
- What is ‘Try-With-Resources’ feature in Java 7?
- Limitation with this feature in Java 7?
- How Java 9 solves that problem?
Let’s begin:
1. What is ‘Try-with-Resource’ feature in Java 7?
Try-with-Resource was introduced in Java 7 and it’s purpose is to close the resource(s) automatically after being used. Let’s say you are using any object implementing java.lang.AutoCloseable or java.io.Closeable interface like File object OR Database object . Now, the objective was to close such resources once their usage is done. Before Java 7, you had to explicitly close such objects in the finally block. But, Java 7 helps to auto-close such resources once used.
Great Huh….
2. Is there any limitation with this feature in Java 7, if yes, what is it?
Yes. There is a limitation here. Challenge is that the resource need to be declared before try or inside the try statement else it will throw the compilation error. See examples below:
Below code will work in Java 7: (because object is declared within try statement)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; public class Try_With_Resouces_Java7_Example { public static void main(String[] args) { String line; Reader inputStringReaderObj = new StringReader("Techndeck.com"); BufferedReader brObj = new BufferedReader(inputStringReaderObj); try (BufferedReader brObj1 = brObj) { while ((line = brObj1.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } |
Below code won’t work in Java 7: (because object is not declared within try statement)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.Reader; import java.io.StringReader; public class Main { public static void main(String[] args) throws FileNotFoundException { String line; Reader inputStringReaderObj = new StringReader("Techndeck.com"); BufferedReader brObj = new BufferedReader(inputStringReaderObj); try (brObj) { while ((line = brObj.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } |
Java 7 Error message:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Main.java:17: error: expected try(brObj) { ^ Main.java:17: error: ')' expected try(brObj) { ^ Main.java:17: error: '{' expected try(brObj) { ^ Main.java:19: error: not a statement while ((line = brObj.readLine()) != null) { ^ Main.java:19: error: ';' expected while ((line = brObj.readLine()) != null) { ^ 5 errors |
3. How Java 9 handle this limitation?
Well, Java 9 improved ‘Try-With-Resources’ and it is no longer needed to declare the object inside the try statement. Lets run the above example(Main.java) in Java 9 now.
It will work completely fine and will generate the below output.
Java 9 Output:
1 | Techndeck.com |
Try-With-Resources Improvement in Java 9...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- dropWhile / takeWhile operations in Java 9 Streams
- Collection sorting using Lambda in Java 8
- Iterate / ofNullable operations in Java 9 Streams