Platinum Partner
java,java 7

New Java 7 Language Features

There were a lot of developers on the party to celebrate Java 7 (afterall, we have been waiting for it for 5 years!).

Last week I was at the Java 7 launch party in Sao Paulo, Brazil and it was really cool. There were a lot of developers on the party to celebrate Java 7 (afterall, we have been waiting for it for 5 years!).

I took some pictures at the event: http://www.flickr.com/photos/loiane/sets/72157627015952961/

Video of the event: http://blogs.oracle.com/java/entry/java_7_celebration_begins

Slides: http://blogs.oracle.com/darcy/resource/ProjectCoin/CoinLaunch.pdf

PrintAnyways, I was really excited with the event and I went home and started to play with Java 7 language features.

As I will have to wait for Open JDK for Mac to be released, I had to play with it on Windows. You can download Java 7 here.

Another issue I found is Eclipse does not have native support for Java 7. They released a plugin, but it still beta. So I decided to play with NetBeans.

The current version of NetBeans is 7, and it already comes with native support to Java 7. If you install Java 7 and then install NetBeans, it will set Java 7 as the default JDK for development.

Then I created a Java Project on NetBeans but the source code was still compiling in Java 6. You have to make a small change to make it compile with Java 7.

All set and I started to play with some languages new features, aka Project Coin.

Strings in Switch:

In all examples, I tried first to write code as we usually do, and then convert the code to a Java 7 feature. The cool thing about NetBeans is it already have tips to convert the code.

First, will compare some Strings using if-else. NetBeans will popup a warning like this:

And if we accept the change, it will convert the code into this:

Following is the code:

Before:

public void testStringInSwitch(String param){

       final String JAVA5 = "Java 5";
       final String JAVA6 = "Java 6";
       final String JAVA7 = "Java 7";

       if (param.equals(JAVA5)){
           System.out.println(JAVA5);
       } else if (param.equals(JAVA6)){
           System.out.println(JAVA6);
       } else if (param.equals(JAVA7)){
           System.out.println(JAVA7);
       }
   }

Java7:

public void testStringInSwitch(String param){

       final String JAVA5 = "Java 5";
       final String JAVA6 = "Java 6";
       final String JAVA7 = "Java 7";

       switch (param) {
           case JAVA5:
               System.out.println(JAVA5);
               break;
           case JAVA6:
               System.out.println(JAVA6);
               break;
           case JAVA7:
               System.out.println(JAVA7);
               break;
       }
   }

Binary Literals:

Before:

public void testBinaryIntegralLiterals(){

        int binary = 8;

        if (binary == 8){
            System.out.println(true);
        } else{
            System.out.println(false);
        }
}

Java7:

public void testBinaryIntegralLiterals(){

        int binary = 0b1000; //2^3 = 8

        if (binary == 8){
            System.out.println(true);
        } else{
            System.out.println(false);
        }
}

And if you try to declare a constant number in the code, NetBeans will warn you to convert it into a Binary Literal:

to

public void testUnderscoresNumericLiterals() {

        int oneMillion_ = 1_000_000;
        int oneMillion = 1000000;

        if (oneMillion_ == oneMillion){
            System.out.println(true);
        } else{
            System.out.println(false);
        }
    }


Underscore Between Literals:

public void testUnderscoresNumericLiterals() {

    int oneMillion_ = 1_000_000; //new
    int oneMillion = 1000000;

    if (oneMillion_ == oneMillion){
        System.out.println(true);
    } else{
        System.out.println(false);
    }
}

Diamond Syntax:

Before:

public void testDinamond(){

    List list = new ArrayList();
    Map> map = new HashMap>();
}

Java7:

public void testDinamond(){
    List list = new ArrayList<>();
    Map> map = new HashMap<>();
}

When we write code in Java 6 syntax, NetBeans will warn us to convert it into Java 7 Diamond Syntax:

To







Multi-Catch Similar Exceptions:

Before:

public void testMultiCatch(){

     try {
         throw new FileNotFoundException("FileNotFoundException");
     } catch (FileNotFoundException fnfo) {
         fnfo.printStackTrace();
     } catch (IOException ioe) {
         ioe.printStackTrace();
}

Java 7:

public void testMultiCatch(){

    try {
        throw new FileNotFoundException("FileNotFoundException");
    } catch (FileNotFoundException | IOException fnfo) {
        fnfo.printStackTrace();
    }
}

Same thing here. NetBeans will ask to convert the code into Java 7:

To











Try with Resources:

Before:

public void testTryWithResourcesStatement() throws FileNotFoundException, IOException{

     FileInputStream in = null;
    try {
        in = new FileInputStream("java7.txt");

        System.out.println(in.read());

    } finally {
        if (in != null) {
            in.close();
        }
    }

}

Java 7:

public void testTryWithResourcesStatement() throws FileNotFoundException, IOException{

    try (FileInputStream in = new FileInputStream("java7.txt")) {

        System.out.println(in.read());

    }
}

Same thing here:

To:

There is also varargs simplification, but I did not understand this one very well to code an example.

There are another features that were submited, but did not make it into this release:

The code presented on this post is not only theory, I really ran it in my machine. You can get/download the NetBeans project from my github: https://github.com/loiane/Java7HelloWorld

 

From http://loianegroner.com/2011/07/new-java-7-language-features/

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}