So, what’s cool with Java 7?

Cosmetic changes

Insights for Java developers

In July this year Oracle announced the availability of Java 7. More than 4 years have passed since the previous release, so what sort of improvements can we expect? Programmers will find several kinds of new features, from cosmetic ones to essential development APIs. Discover more here…








Cosmetic changes

Java 7 has enhanced its grammar to ease developers' everyday pains:

Using strings in switch statements

Finally, Java now allows to use strings in case statements:

String color = user.getColorString ();

int dummy = -1;

switch (color) {

         case “white”:

                  dummy = 0;                                 



         case “black”:

                 dummy = 1;                                 




                 break;                 dummy = 2;                                 


Diamond operator

Working with Generics can be (too) verbose. In order to avoid the redundancy, the <> operator (aka diamond) can be used in place of the complete definition:

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


Would simply become:

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


However, it would be easier to read as:

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


Perhaps we should suggest it for Java 8...


Numeric and binary literals

Ever needed to describe large binary numbers? Java 7 is now offering a nice “0b” prefix to create them:

int n = 0b100010;  // n = 34

We frequently use a separator when writing large numbers - be it commas or spaces -  well Java 7 uses underscores. A bit unconventional, but hey, at least it works! However, do not start or end numbers with an underscore or place them around decimal points...

int oneMillion = 1_000_000;         // simple to read isn’t it?

int anotherMillion = 1__00__00_00; // at least it compiles
int hexaMillion = 0x1_000_000;      // available with any type J


The try-with-resources statement

This addition reduces the code length however, also provides an interesting auto-closing of resources like InputStream.  Take a look at the following example:

// Note the parenthesis block right after the 'try' command

try (FileInputStream fileStream = new FileInputStream("readme.txt") ) 
        String currentLine =;
} catch (IOException e) {
         // error handling …

 No more finally block since the fileStream.close()is automatically called. This is true for any class implementing the java.lang.AutoCloaseable interface.


Handling of multiple exceptions in catch block

This is a smart way to reduce the size of the catch blocks. It is using the usual or operator, which makes you feel like you have always used it.

try {

       // your code here ...

} catch (IOException | SQLException e) {

         // error handling

} catch (NullPointerException en)
         // multi catch still possible


Major changes in the Java language

The internal engine is providing new classes or API for a better integration with modern OS.

File Management and Monitoring

The java.nio.file.Files class and the WatchService API brings very useful programming means to manipulate files and to monitor their changes:

Path path = FileSystems.getDefault().getPath("helloWorld.txt");

if (Files.exists(path) ) {
    // move, copy, createSymbolicLink when compatible …  


Fork and Join

Last but not least, Java 7 is introducing a Fork and Join framework to ease the programming of distributed tasks.  Knowing that almost all processors today are multi-core (up to cell phones), it is really beneficial to break batches of work into smaller recursive calls.

See the ForkJoinPool and ForkJoinTask classes for more details.



There are even more features inside Java 7, but I wanted to focus on the most

 visible/most important ones.  Some will improve programmers’ activities whilst others will allow you to further take advantage of modern hardware.

All in all, this is not a revolution, but rather a welcome evolution.

Digital news!

Are you as digitally addicted as we are? We can supply you with a regular dose of digital news. Simply sign-up, or click to Follow us online.

About the author

Stéphane Bernigaud