Quick 20 minutes look at Java 7 new features


Oracle .Java 7 new features. People who want to have a taste of it, below are the list of new features.


Project Coin – These are small enhancements in language itself


1. Strings in switch statements.

This is syntactic sugar and compiler magic. There is no loss of performance, at background compiler transform below to “int” switch


switch(s) {

case “a”:

case “b”:

case “c”:

return 10;

case “d”:

case “e”:

case “f”:

return 20;

default :

return 30;



2. Try with resources and Autocloseable interface

Now all IO streams and JDBC interfaces extends Autocloseable interface. You don’t need to close stream or connections explicitly. Java “try” block will automatically do that for you. Again no loss of performance, it is compiler magic similar to the one in Generics and in above switch statement


public static String readFirstLine(String file) throws IOException {

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {

return reader.readLine();




  1. Diamond Operator- improved type inference for generic instance creation


List<String, Map<String, String>> list2 = new ArrayList<>(); // less coding at right has side because of diamond operator


4. Multi catch

try {    // some stuff} catch (SQLException | IOException | SomeOtherException e) {    log(e)}



http://www.oracle.com/us/corporate/events/java7/index.html – Look for video link “Making Heads and Tails of Project Coin, Small Language Changes in JDK 7”




File System API

Extended support to file system managed by OS including USB drives. Following are new interfaces and classes in “java.nio.file” package


1. Path – Potential replacement of java.io.File. Support for symbolic links

2. Files – Utility methods similar to that in java.util.Collections class. With single method call, you can read, create, copy, delete files (or folders) and access/alter their attributes and permissions

3. FileSystem – Handle to underlying file system.

4. FileStore – Underlying Storage – device, partition, volume, concrete file system or other implementation specific means of file storage. You can access total size, free space etc.

5. Watchable and WatchService – Any change in file system can be observed e.g. creation and deletion of files in folder

6. DirectoryStreams – An object to iterate over the entries in a directory



http://www.oracle.com/us/corporate/events/java7/index.html – Look for video link “The New File System API in JDK 7”





Fork/Join framework – Utilizing Multi core architecture


JDK 5 introduced Executor framework. It work well under following conditions

  1. Coarse grain parallelism, typically –one user request per task
  2. Small number of cores, and 2-8 cores


But executors don’t scale well for large number of cores. 64 to128 core servers are standard today. The shared queues and other infrastructure used by executors and thread pools becomes a point of contention and reduce scalability. Fork/Join framework address this problem (to keep the all CPU busy).


Following are salient points

– You need to customize the algorithm to “Divide and Conquer” – see references below (If you cannot change algorithm, you cannot use this frame work)

– It is specifically designed for computing intensive problems

– It uses work stealing concept (if some CPU is free it can steal work from other busy CPU)

– Programming logic should not involve File IO or Network IO


Its classes are part of java.util.concurrent package. Following is a classical usage to compute Fibonacci number


class Fibonacci extends RecursiveTask<Integer> {   static final ForkJoinPool mainPool = new ForkJoinPool();   final int n;   Fibonacci(int n) { this.n = n; }   Integer compute() {     if (n <= 1)        return n;     Fibonacci f1 = new Fibonacci(n – 1);     f1.fork();     Fibonacci f2 = new Fibonacci(n – 2);     return f2.compute() + f1.join();   }    public void sort(long[] array) {        mainPool.invoke(new Fibonacci(2048));   }  }



http://www.oracle.com/us/corporate/events/java7/index.html – Look for video link “Divide and Conquer Parallelism with the Fork/Join Framework”






Look at one of criticism I came across on net about Fork/Join




The Vinci Project – Invokedynamics


This is the most important feature of this release. It will changes the way people perceive JVM. Traditionally java is the only language supported by JVM. It is a statically typed language as oppose to groovy, ruby or java script. It means that types for variables, method arguments and return should be known before runtime. As an example


//java code

String name = “rajan” // type is defined before hand


Where as in dynamic languages, we don’t need to define the type before


//java script code

var count = jQuery(“p”).length // type of variable count will be discovered during runtime


//groovy code

def users = User.findAll() // type of users is again not defined here


Even we can define methods after declaring and initializing the objects. Below is very trivial example using java script/JQuery  (Jquery is just a list of functions)


//java script and html snippet

<a href=”#” id=”some-link”>Click Me!</a>


jQuery(‘# some-link’).bind(‘click’, function(){

//do something




Above I am binding the ‘click’ functionality during runtime. And after some time during execution, I can also decide to unbind the click function handler (using jquery unbind() method). So methods are actually first class objects, reference of those I can change at runtime.


To implement features like above, language designer needed to perform lot of simulations when they constructed language over JVM (Java Script – Rhino, JRuby, Jython, Groovy). These simulations made program run slow. This is one of the primary reason people say that groovy is very slow.


Enter the world of “invokedynamic”. It is a new instruction for JVM, similar to instructions like invokestatic, invokevirtual etc. (that jvm uses). Now it is possible to implement above features natively in JVM.


What does it mean to me? As java developer – nothing!!!!. (apart from some new classes in java.lang.invoke package).


But as user of dynamic languages like groovy, scala, jRuby, jython – These dynamic languages will be as fast as java, and they can make use of JIT (just in time) / Hotspot technologies efficiently.



http://www.oracle.com/us/corporate/events/java7/index.html – Look for link “A Renaissance VM: One Platform, Many Languages”




By jobshala Posted in Others