JAVA 7 Features

JAVA 7 Features

Key Features

  • Strings in switch Statement
  • Multiple Exception Handling
  • Try with Resources
  • Java nio Package
  • Binary Literals, underscore in literals
  • Diamond Syntax
  • Many others refer to Oracle Release note

JAVA 7 : Code named Dolphin and released on July 28, 2011.

Release Note
Oracle Release Note
Feature Page

Support of String in Switch Case.

String in Switch support are useful for processing data without conversion to enum or if-else logic. Sometimes is just easier to switch on String.

The switch statement when used with a String uses the equals() method to compare the given expression to each value in the case statement and is therefore case-sensitive and will throw a NullPointerException if the expression is null

Binary Literals
Binary literals are new features in Java 7. As you all know that we can write integral types (byte, short, int, and long) in Binary and Hexadecimal formats but from Java 7 onwards we can write these numbers in binary format also. The number should be prefixed with 0b or 0B to be treated as binary literal.

This feature is very helpful to bit-oriented systems like processors, network protocols and bitmapped hardware device. Early the programmer used to transform from binary to decimal/hexadecimal and vice versa. Using this feature will remove this transformation and chances of error will be less in this conversion.
ex :

Underscore Between Literals
In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

Diamond Syntax/Operator
The diamond operator is a nice feature as you don’t have to repeat yourself. It makes sense to define the type once when you declare the type but just doesn’t make sense to define it again on the right side.

The Diamond Operator reduces some of Java’s verbosity surrounding generics by having the compiler infer parameter types for constructors of generic classes.

Catching Multiple Exception Types
In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.
Consider the following example, which contains duplicate code in each of the catch blocks:

In releases prior to Java SE 7, it is difficult to create a common method to eliminate the duplicated code because the variable ex has different types.

The following example, which is valid in Java SE 7 and later, eliminates the duplicated code:

The catch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|).

Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block.

Bytecode generated by compiling a catch block that handles multiple exception types will be smaller (and thus superior) than compiling many catch blocks that handle only one exception type each. A catch block that handles multiple exception types creates no duplication in the bytecode generated by the compiler; the bytecode has no replication of exception handlers.

Rethrowing Exceptions with Improved Type Checking
The Java SE 7 compiler performs more precise analysis of rethrown exceptions than earlier releases of Java SE. This enables you to specify more specific exception types in the throws clause of a method declaration.

Consider the following example:

This examples’s try block could throw either FirstException or SecondException. Suppose you want to specify these exception types in the throws clause of the rethrowException method declaration. In releases prior to Java SE 7, you cannot do so. Because the exception parameter of the catch clause, e, is type Exception, and the catch block rethrows the exception parameter e, you can only specify the exception type Exception in the throws clause of the rethrowException method declaration.

However, in Java SE 7, you can specify the exception types FirstException and SecondException in the throws clause in the rethrowException method declaration. The Java SE 7 compiler can determine that the exception thrown by the statement throw e must have come from the try block, and the only exceptions thrown by the try block can be FirstException and SecondException. Even though the exception parameter of the catch clause, e, is type Exception, the compiler can determine that it is an instance of either FirstException or SecondException:

Try with Resources
The try-with-resources statement is a try statement that declares one or more resources. A resource is as an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.