Share

From this page you can share Java 7 Arrives to a social bookmarking site or email a link to the page.
Social WebE-mail
Enter multiple addresses on separate lines or separate them with commas.
Java 7 Arrives
(Your Name) has forwarded a page to you from Ajaxonomy
(Your Name) thought you would like to see this page from the Ajaxonomy web site.

Java 7 Arrives

Tagged:  

5 years since its last release, a new Java release finally gets out of the gates. Though less ambitious than the initial plans for the new release, it is shipping now--and, as a wise man once said, shipping is a feature. Oracle clearly recognized this after taking over at the helm of Java, and paired down the feature list in order to get some of the momentum that Java had lost in the waning years of Sun. Java 7 is hence being billed as an evolutionary release (that's code for "no lambdas yet"); nevertheless it is a much bigger step forward than Java 6 was, and a push that the platform really needed.

So, without further ado, I'll cut to the chase and give a run-down of what the new features are and an assessment of their general usefulness.

1) The fork/join framework, also known as JSR-166y. Fork/join is well-optimized set of concurrency classes designed for fine-grained parallel computation. Fork/join is useful when you have computation tasks that are easily separable to run in parallel; a good example would be the class of algorithms which use a "divide and conquer" strategy such as quicksort. The framework also introduces the Phaser class, a more flexible version of a CyclicBarrier or CountDownLatch.

Missing in action from the original library developed by Doug Lea is ParallelArray and friends, namely because without lambda expressions (coming in Java 8) a ridiculous number of classes are needed to make it work. Presumably a slimmer version will be included with Java 8.

2) The invokedynamic bytecode instruction. Though most Java programmers won't even know it's there, this new instruction is a powerful extension to the JVM, and will be the great enabler for dynamic languages such as JRuby or Jython. Java doesn't miss all the fun, though: lambda expressions in Java 8 may well be compiled down to invokedynamic instructions, according to Brian Goetz.

3) Project Coin. Project Coin contains lots of small but highly useful changes, including:

  • The diamond operator for generics. This introduces more visible type inference into Java, albeit in a simple way:
    Map<String,Object> m = new java.util.HashMap<>();
  • Try-with-resources. This features introduces a new type of try block that auto-closes resources at the end of the block. To do this the AutoCloseable interface was introduced and a number of existing class retrofitted with it. Essentially, try-with-resources removes the need for a finally block to close the resource by passing an AutoCloseable as an argument to the try:
    try (Statement stmt = con.createStatement()) {
         // ... 
    } catch(Exception e) { ... }
  • Strings in switch statements. Strings. In switches. 'Nuff said. Handy if you still use switch statements in your code.
  • Multi-catch. Java 7 now allows you to define a catch block with more than one exception type. This is especially handy when using Java reflection, which for some reason seems to throw a lot of them:
    try {
        Class.forName("java.lang.Object").newInstance();
    } catch (ClassNotFoundException |
             InstantiationException |
             IllegalAccessException e) {
        e.printStackTrace();
    }
    

4) NIO.2 (JSR-203). Among its key features is a much-needed overhaul of Java's file system API. The centerpiece of this new API is the Path class, which supercedes the older java.io.File implementation. Among Path's new methods is a highly useful relativize(), which allows the simple calculation of the relative path of from one Path to another:

FileSystem local = FileSystems.getDefault();

Path p1  =  local.getPath("/path/to/file");
Path p2 = local.getPath("/path/to/another/file");
Path rel = p2.relativize(p1);  // rel is "../file"

Path is also Watchable, enabling the polling of the file for changes:

//Thread for polling the filesystem for changes
public class Watcher implements Runnable {
   public void run() {
     FileSystem local = FileSystems.getDefault();
     WatchService watchService = local.newWatchService();
     Path watched = local.getPath("path/to/dir");
     WatchKey regKey = watched.register(watchService,
         ENTRY_MODIFY, ENTRY_CREATE, ENTRY_DELETE);
     // poll in infinite loop
     while(true) {
         WatchKey key = watchService.take();
         for (WatchEvent<?> event: key.pollEvents()) {
              //...process events
         }
     }
   }
}

Other nice additions include:

  • An API for walking the directory structure using a FileVisitor
  • The ability to query file attributes in Unix, DOS, or NTFS
  • A file system provider for Zip compression
  • ...oh yes, you can now also simply copy a file by telling the OS to, rather than reading and writing the data yourself (what took so long on this one?).

5) Elliptic Curve Cryptography. For years RSA was the only game in town for asymmetric public key cryptography. Now we have ECC as well, as a native provider for it was added to Java 7.

6) Stream Control Transport Protocol (SCTP). SCTP, a standard created by the IETF, is a reliable message-oriented protocol (similar to its less reliable cousin, UDP). SCTP has the following key features:

  • Session-oriented
  • Ordered and unordered message delivery
  • Multi-homing (each endpoint may have multiple IP addresses): provides failover and redundancy
  • Multi-streaming: data can be partitioned into multiple streams with independent sequenced delivery
  • Eliminates TCP's head-of-line blocking problem

A comparison of SCTP with other transport-layer protocols is here. SCTP in Java 7, however, is not yet a part of Java's public API (it's in the com.sun.nio.sctp package) as it was felt that more industry experience was needed to shape the API correctly.

There's lot of other small improvements in Java 7 that I haven't covered here. For a full list of new features, refer to the appropriate documentation.

A Word of Caution

Java 7 shipped with a loop optimization bug that many consider to be show-stopping bugs (apparently Oracle did not, as they were found 5 days prior to the release). The bugs are in one of the Hotspot's experimental loop optimizations, which was disabled in Java 6 (enabled using -XX:+OptimizeStringConcat or -XX:+AggressiveOpts), but enabled by default in Java 7. While these bugs caused much flap in the wake of the Java 7 release, the workaround is simple: use the -XX:-UseLoopPredicate flag to disable it.

Still to come in Java 8...

  • Lambda expressions (not-quite-closures but close enough)
  • Support for Java language modularity: Jigsaw as well as JVM-level changes
  • Literal expressions for collections
  • Anotations on Java types (JSR-308)
  • And more...