Tuesday, August 2, 2011

Java 7 Summary of New Features

Here is a summary of the new features released in Java 7 as part of Project Coin language enhancements are:
  1. Improved Numeric Literals - Binary integral literals and Underscore in numeric literals
  2. Strings in switch
  3. Diamond operator or simpler type inference for generic instance creation
  4. try-with resource statement
  5. Multi-catch and more precise throw
  6. Simplified varargs method invocation
  7. Fork & Join enhancements
  8. NIO 2.0 & File system access enhancements
These are explained in detail below.

Improved Numeric Literals
Integral types (byte, short, int, and long) can also be expressed using the binary number system by prefixing "0b" or "0B" to the number's binary representation as:
int binaryLiteral = 0b110011

You can have underscores in numeric literals to make them more readable like:
int tenMillion = 10_000_000;

Strings in switch
Till Java6, only numbers or enums could be used in switch statements. Now strings are also allowed:
String switchString = getStringValue();
switch(switchString) {
    case "foo": doFoo();
                break;
    case "bar": doBar();
                break;
    case "fuu": doFuu();
                break;
    default: doDefault();
             break;
}
Diamond operator
This is a short cut to reduce typing and make it simpler to infer generic types during instance creation
Map<String, List<Pair<String,BigDecimal>> myMap = new HashMap<>();

Simplified varargs method invocation
When a programmer tries to invoke a *varargs* (variable arity) method with a non-reifiable varargs type, the compiler currently generates an "unsafe operation" warning. This proposal moves the warning from the call site to the method declaration. This merits more detailed explanation and a separate post.

Multi-catch and more precise throw
Now you can catch multiple exceptions in the same catch clause and also re-throw final exceptions without declaring the method with the "throws" clause.  More Precise Rethrow allows you to catch a high level exception instead of each possible exception, but when you re-throw this caught exception, the correct sub-class is thrown instead of the higher level exception. This merits more detailed explanation and a separate post.


Fork & Join threading enhancements
New Fork / Join framework as an implementation of the ExecutorService to help take advantage of mulit-core / multi-processor machines.


NIO 2.0 & File system access enhancements
NIO 2.0 & File system related changes make it easier for traversing file system trees and access file properties.
There are numerous changes and these could be a good candidate for a separate book in itself.


Check out the Summary of Java 5 features that are also hold good and are very useful.

No comments:

LinkWithin

Related Posts Plugin for WordPress, Blogger...