Java7 features – Whats new?

[Social9_Share type="horizontal"]

Java7 Features
In this blog, let us see few of the features of java7.

  • Use of Strings in switch case
  • Try with resources
  • Automatic Resource Management
  • Binary Literals
  • Diamond Operator

1.Use of Strings in switch case
Java has added a language level support for String in switch. Now you can rewrite the same code more elegantly
Java 1

The new feature of Java7 introduced in exception handling is try-with-resources. This technique, helps to close the resources automatically, after the application gets completed.The use of try-with-resources removes the work of adding finally in your code for closing the resources
A resource can be a file or a connection object or a scanner object or any class that implements java.lang.AutoCloseable or java.io.Closeable.

Exception Handling before java7
The resources must be closed to avoid resource leakage in any java application before Java7. you have to explicitly close the resources
Java 2

Here the reader object is closed explicitly in the finally block.

Exception Handling using try-with-resources in Java7
In Java7, try-with-resources feature allows you to close the resources automatically without using close() method. Let us modify the above construct using try-with-resources.
Java 3
The above example creates two resources a FileReader and a FileWriter object inside the parentheses after the try keyword. Both the resources will be closed automatically after the completion of try block.
The resources will be closed in reverse order of the order in which they are created inside the brackets. In our example writer object will be closed first and then the reader object.

3.Automatic Resource Management:
Java7 enables the automatic closing of resources. A class that implements java.lang.AutoCloseable or java.io.Closeable can be considered as a resource. This can then be used with try-with-resources construct.
An implementation of this
Java 4
In the above example MyResource is a class which implements AutoCloseable and overrides close() method. myMethod() is the own method of MyResource class
Now this class can be invoked within try-with-resources construct
Java 5

So with the use or try-with-resources, the code is safe in handling the exception and closing the resources without finally block.

4.MULTI CATCH in Exception Handling

Multiple Catch prior Java7
A single try block can have multiple catch blocks. When a single try block has statements that generate different type of exceptions, multi catch is used. You might have seen try with multiple catch statements. Here the sub class exceptions are caught first and then the super class exceptions. eg.
Java 6
If you try to handle super class exceptions first and then the sub class exceptions, it will give a compiler error saying unreachable code. eg.
Java 7

Multi catch
In Java 7, multiple catch statements can be written as a single statement. eg.

Java 8

In the above example, if any of the exceptions are thrown from the try block, it is handled inside the single catch statement.

Java 9

But, if the exceptions are in hierarchy, then you cannot use multi catch. If your catch is handling ArithmeticException (sub class) and RuntimeException (super class) then multicatch is not possible. It throws a compiler error saying he exception ArithmeticException is already caught by the alternative RuntimeException.
Java 10

5. Binary Literals
Java7 accepts binary literal values also for int, short, long data types.

Java 11

6. Diamond Operator
While using classes of collection framework with generics, the use of Diamond Operator comes into picture. This helps to reduce the verbosity which occurs while using generics code

Using ArrayList prior Java7

List months = new ArrayList < String > ();
List empList = new ArrayList();


List months = new ArrayList <> ();
List empList = new ArrayList<>();

We will see the other features in the next blog.

- By Madheshwaran

Happy Learning


  1. Reply
    kuldeep says

    It helped me while going through JAVA training.

Post a comment