Loiane Groner, Brazilian, works as a Java/ Sencha evangelist. She has 7+ years of experience in web development. She is the ESJUG (Espirito Santo Java Users Group) and CampinasJUG (Campinas Java Users Group) leader and coordinator. Loiane is passionate about technology and programming. Also author of ExtJS 4 First Look book. Loiane is a DZone MVB and is not an employee of DZone and has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

New Java 7 Language Features

07.11.2011
| 51062 views |
  • submit to reddit

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/

Published at DZone with permission of Loiane Groner, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Tomasz Nurkiewicz replied on Tue, 2011/07/12 - 1:22am

In testDinamond() less-then/greater-than symbols are incorrectly escaped.

Sivaprasadreddy... replied on Tue, 2011/07/12 - 1:27am

Hi,

Multi-Catch Similar Exceptions is a cool one and will save lot of developers time.

Thanks,

Siva

Paul Harvey replied on Tue, 2011/07/12 - 4:01am

Most of the new language features seem to be little more than syntactic sugar - the speed of software development is not limited by the how quickly we can type - but rather by how quickly we can think. So in this respect I doubt that multi-catch, although welcome, will have too much influence.

The try-with-resources mechanism looks to be the only of the new language features that will have any significant impact in terms of increased productivity. This because it will eliminate bugs by making resource management less error prone.

Read a little more here and my burgeoning Blog on software development here.

Matt Young replied on Tue, 2011/07/12 - 12:15pm in response to: Paul Harvey

I disagree. Yes they may only be syntactic sugar but they will result in tighter code. Tighter code = Less confusion when debugging. I have always said that the difference between good programmers and great programmers is how fast problems are resolved. Anything that eliminates overly-wordy code, will make you more productive. I spend far more time reading code than writing it. The less I have to read, the better I will be. BTW if you wanna have a discussion about someone else's writing, have the discussion there. Don't try to hijack the discussion by redirecting the discussion to your ad-laden personal blog. Show the author who inspired the discussion the respect of contributing to their article by having the discussion there.

Paul Harvey replied on Tue, 2011/07/12 - 1:40pm in response to: Matt Young

I did not mean to cause offence ... I am very happy to debate the issues at hand. I certainly meant no disrespect to anyone.

There are no ads whatsoever in my Blog ... so I am little confused there. I'd already written the Blog article on the the Java 7 launch event so I thought it efficient and relevant to link it in here.

But to the actual technical issue at hand:

I agree that the new language features are welcome ... they are just not going to set the world on fire. In terms of good coding I'd say that the most important features nowadays are algorithmic efficiency and clarity of expression. Unneeded verbosity is distracting - so appropriate language constructs are welcome here - however inappropriate terseness is positively harmful in that it makes the code more difficult to understand for others, I do NOT claim that any of the new language features will lead to inappropriate terseness. You have likely seen, like myself, many times where inexperienced programmers have written some code they deem to be "clever" but in actuality it is a maintenance nightmare because they have not expressed themselves clearly in code but rather have honed the code to the most terse possible form. I stick to to the premise that speed of typing (or indeed reading) is not the limiting factor in writing (or understanding) code.

Matt Young replied on Tue, 2011/07/12 - 2:50pm in response to: Paul Harvey

clearly I think this java release leaves a lot to be desired. If that is your point, I would agree. I think passing on features like closures and null-safe navigation puts java further behind other languages and leaves room for other platforms to call java "legacy", or "the new cobol". At the end of the day, Java again proved that the JCP is killing innovation. The good news is that we still have groovy. I am not a fan of groovy because of how poorly it integrates with existing build architectures but you cannot deny the overwhelming power of it compared to java. I had high hopes that the long development cycle of this release meant that java was working to make groovy obsolete but instead java 7 feels like a java 6 minor revision. I for one am tired of the JCP aborting good ideas because they have their own pet fiefdoms to protect. The good news is that spring and groovy communities seem more than willing to pick up the slack and either java 8 will be right around the corner or groovy will replace it altogether. As to your blog, I didn't go to it because I have a thing against people plugging their own blogs in article comments. I simply won't follow those links out of principle.

Loren Kratzke replied on Tue, 2011/07/12 - 6:19pm in response to: Matt Young

Dude, relax. If they created a foolproof language then somebody would just build a better idiot. You know that. The link to his blog was totally relevent and on topic (with no ads). Everybody agrees that the JCP is broken though. On this we all agree.

Satish Thakur replied on Wed, 2011/07/13 - 12:41am

nice writeup.

Diamond syntax will really reduce the length of line. 

Try with resources is really cool feature.

Atul More replied on Wed, 2011/07/27 - 7:33am

I agree with Matt and Paul but my view is little diffrent. I think ... even if we find a solution of a problem quickly but at implmentation level the technology/language support is also important to implement the idea quickly.

Gym Prathap replied on Fri, 2013/05/03 - 10:44pm

If any computer contains multiple processors, then we use the new feature of Java 7 Fork Join Framework.

Java Training in Chennai 

Arjun Rishi replied on Mon, 2013/12/09 - 6:18am

above 7 language feature support all system? any other special source needed?

by

Arjun Rishi


Kevin Wolf replied on Mon, 2014/02/10 - 5:26pm

Interesting, but the Java 8 revisions turns some of this upside down.  There's a blog post here about recent programming trends, and Java's current position.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.