It didn’t contain the much hyped lambda expressions for example. Still, it did bring a lot of other neat additions to the Java language. You can see the more at http://docs.oracle.com/javase/7/docs/, or read on for my summary.

The highlights of Java7 included:

Project coin

Strings in switch

try-with-resources statement

Multi-catch and more precise rethrow

The diamond operator

Binary integer literals

Underscores in numeric literals

New Input/Output features

Fork & Join / Concurrency utilities

Project Coin

Project coin, a pun on ‘small change’, contain a few minor but useful features, including:

Strings in switch

Pre Java 7, switch statements worked either with primitive types or enumerated types.

Java 7 introduced another type that we can use in Switch statements: the String type.

Say we have a requirement to process a Trade based on a String status field. Until now we used to do this by using if-else statements.

The declaration of the resources appears within parentheses immediately after the try keyword.

Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource. Resources will be closed regardless of whether the try statement completes normally or abruptly

More precise rethrow

The Java SE 7 compiler performs more precise analysis of rethrown exceptions, enabling you to specify more specific exception types in the throws clause of a method declaration than are used in the catch/throws of the method body.

The diamond operator

The diamond operator simplifies constructor calls involving generics.
Say you wanted to create a map of String IDs to trade Objects. The old way would have been:

Map<String, Trade> trades = new TreeMap<String, Trade>();

But the right-hand side seems a bit redundant. Could the compiler infer the types by looking at the left-hand-side declaration? In Java7 it can, and the code becomes:

Map<String, Trade> trades = new TreeMap<>();

This is a nice but minor convenience – it could already be nicely dealt with using the Guava library. For example:

Map<String, Trade> trades = Maps.newTreeMap();

Binary integer literals

Useful if you are dealing with binary in your code
For example, you can now do:

int three = 0b101;

Note the 0b (or 0B) prefix to identify the number as a binary literal.

Allowing underscores in numeric literals

I don’t think this one is too significant either, but could be useful in some circumstances
e.g.

long creditCardNumber = 1234_5678_9012_3456L;

Fork & Join / Concurrency utilities (JSR 166)

The Fork/Join framework focuses on using all the processing resources available in the machine to improve the performance of the applications. It is designed for work that can be broken into smaller pieces recursively (‘Divide and Conquer’ algorithms).

Summary:

Fork/Join Framework: allows for easier Parallel Programming in Java

Targeted at multi-processor systems (really almost all hardware today)

Use in situations where

a batch of work can broken into smaller recursive calls.

also uses a work-stealing algorithm where threads with no work can steal available work from other threads that are busy

Java 1.4 and before:
Developing concurrent classes was very difficult — the low-level concurrency primitives provided (synchronized, volatile, wait(), notify(), and notifyAll()) were difficult to use correctly, and errors using these facilities difficult to detect and debug

ExecutorService: An Executor that provides methods that produce a Future that represents (as yet to be completed) result of an asynchronous computation.

Java 7:

ForkJoinPool: a new type of ExecutorService, which allows you to more easily break up processing to be executed concurrently, and recursively executes ForkJoinTasks: A thread-like entity that is much lighter weight than a normal thread. Huge numbers of tasks and subtasks may be hosted by a small number of actual threads in a ForkJoinPool.

The new ForkJoinPool employs a divide and conquer algorithm.

Pseudocode (from Doug Lea’s paper on the subject):

Result doWork(Work work) {
if (work is small) {
process the work
}
else {
split up work
invoke framework to solve both parts
}
}

Summary

nice to see continued advancement in Java in the concurrency space to build on all the goodies we got in Java 5

potential uses of this framework are limited and require a fairly narrow problem scope.

New Input/Output features

Java7 introduced a new file I/O library to enhance platform independence and add support for metadata and symbolic links. The new packages are java.nio.file and java.nio.file.attribute. The primary new classes are:

Path: “a programmatic representation of a path in the file system”. This is probably the new class (interface) that developers will use most often. The file referred by the path does not need to exist. For all practical purposes, you can think of replacing java.io.File with java. io.Path. It includes various methods that can be used to obtain information about the path, including creating, converting and comparing paths. Note that an associated helper class, Paths, contains a couple of static methods to convert a string or URI into a Path.

Files: The Files class offers a large number of static methods for file related operations e.g. reading, writing, and manipulating files and directories. The Files methods work on instances of Path objects.

That’s it! A summary of what Java7 gave us. See my next blog post for a review of some of the upcoming features in Java8.