Friday, June 8, 2012

Java 1.7 New Features Over 1.6

Automatic Resource Management

Description: A proposal to support scoping of resource usage in a block with automatic resource cleanup. This addresses the common and messy problem of correctly closing or releasing resources in a finally block, particularly when needing to close multiple resources in the same block.


BufferedReader br = new BufferedReader(new FileReader(path));
try {
   return br.readLine();
} finally {


try (BufferedReader br = new BufferedReader(new FileReader(path)) {
   return br.readLine();

// Start a block using two resources, which will automatically
// be cleaned up when the block exits scope
do (BufferedInputStream bis = …; BufferedOutputStream bos = …) {
… // Perform action with bis and bos

You can declare more than one resource to close:

try (
   InputStream in = new FileInputStream(src);
   OutputStream out = new FileOutputStream(dest))
 // code

Underscores in numeric literals

int one_million = 1_000_000;

Strings in switch

String s = ...
switch(s) {
 case "quux":
    // fall-through
case "foo":
  case "bar":
case "baz":
    // fall-through

Binary literals

int binary = 0b1001_1001; 

Improved Type Inference for Generic Instance Creation

Map<String, List<String>> anagrams = new HashMap<String, List<String>>();


Map<String, List<String>> anagrams = new HashMap<>();

Better exception handling


} catch (FirstException ex) {
     throw ex;
} catch (SecondException ex) {
     throw ex;


} catch (FirstException | SecondException ex) {
    throw ex;

Language level XML support

Description: Add support for multi-line XML (or possibly other languages or kinds of text) to be embedded within code.



Big Decimal operator support

Description: Add support for manipulating BigDecimal objects with arithmetic operators (just like other numeric primitives) instead of via method calls.

Enhanced null handling

Description: Null-ignore invocation is concerned with dealing with possible null values in calling one or especially a chain of methods.

For example, instead of 

public String getPostcode(Person person) {
if (person != null) {
Address address = person.getAddress();
if (address != null) {
return address.getPostcode();
return null;

you could instead call:

public String getPostcode(Person person) {
return person?.getAddress()?.getPostcode();

New file system API (NIO 2.0)

File change notifications -> The WatchService API lets you receive notification events upon changes to the subject (directory or file).

The steps involved in implementing the API are:

  • Create a WatchService. This service consists of a queue to hold WatchKeys.
  • Register the directory/file you wish to monitor with this WatchService.
  • While registering, specify the types of events you wish to receive (create, modify or delete events) 
  • You have to start an infinite loop to listen to events 
  • When an event occurs, a WatchKey is placed into the queue 
  • Consume the WatchKey and invoke queries on it.

Date and Time API

Description: This JSR will provide a new and improved date and time API for Java. The main goal is to build upon the lessons learned from the first two APIs (Date and Calendar) in Java SE, providing a more advanced and comprehensive model for date and time manipulation.
The new API will be targeted at all applications needing a data model for dates and times. This model will go beyond classes to replace Date and Calendar, to include representations of date without time, time without date, durations and intervals. This will raise the quality of application code. For example, instead of using an int to store a duration, and javadoc to describe it as being a number of days, the date and time model will provide a class defining it unambiguously.

Java 1.7 New Features Over 1.6

Automatic Resource Management Description: A proposal to support scoping of resource usage in a block with automatic resource cleanup. T...