You can get with this, or you can get with that

Java 7 offers some small changes (Project Coin — get it? get it?) to the language.  Here's an illustration of some of them.

Switch on Strings

Before, you had to do this:

    switch(someInt) {
        case 0: return zeroIntResult;
        case 1: return oneIntResult;
        default: return new BrokenSwitchException("Bad Integer");
    }

In Java 7, you can do this instead:

    switch(someString) {
        case "Bubba": return bubbaStringResult;
        case "Sue": return sueStringResult;
        default: return new BrokenSwitchException("Odd String!");
    }

Try with Resources

Before, you had to do this:

    try {
        // perform some op that opens resources
    } catch (AnyExceptionThrownByThatOp aetbto) {
        // handle the exception byfm
    } finally {
        // close the resources that were opened for that op
        try {
            // yes, try to close 'em.  Might fail.
        } catch (Exception ignored) {
            // See? Might fail. Nothing to do here.
        } // Wow. This is verbose!
    }

Ugly and verbose. But with Java 7, you can specify resources to open for the try, and they'll be automagically closed no matter what happens:

    try (FileOutputStream fos = new FileOutputStream(file)) {
        // perform some op using the stated resources
    } catch (AnyExceptionThrownByThatOp aetbto) {
        // handle the exception byfm
    } // Nothing to do. The FileOutputStream will be autoclosed.

Exception Multi-catch

Sometimes, an operation could throw any of, say, three Exceptions, and two of them could be of a sort you'd like to handle in the same way. Before, you had to do this:

    try {
        // perform some op that can throw various exceptions,
        // where some subset of them should be handled "thisway"
    } catch (FirstSimilarException fse) {
        // deal with it thisway(fse)
    } catch (SecondSimilarException sse) {
        // deal with it thisway(sse)
    } catch (Exception ex) {
        // deal with it thatway(ex)
    }

In Java 7, you can consolidate the handling of similar exceptions by catching them at once:

    try {
        // perform some op that can throw various exceptions,
        // where some subset of them should be handles "thisway"
    } catch (FirstSimilarException | SecondSimilarException e) {
        // deal with it thisway(e), whatever it is
    } catch (Exception ex) {
        // deal with it thatway(ex)
    }

Diamond Syntax (type inference for generics)

Before, you had to do this:

    Map> aMap = new HashMap>();

In Java 7, you can do this instead:

    Map> aMap = new HashMap<>();

Underscores Between Digits

Before, you had to do this:

    long creditCard = 5440444033300005L;
    long phoneNumber = 2135551212L;

In Java 7, you can do this instead:

    long creditCard = 5440_4440_3330_0005L;
    long phoneNumber = 213_555_1212L;

The underscores are semantic fluff, but the syntax is eye-friendly.

Binary Literals

Before, you had to do this:

    int x = Integer.parseInt("1010110", 2);

In Java 7, you can do this instead:

    int x = 0b1010110;

Other stuff

"And there may be many others, but they haven't been discovered."

You might also like:

This entry was posted in Releases and tagged . Bookmark the permalink.

4 Responses to You can get with this, or you can get with that

  1. Abani says:

    Nice article. want to see more.. thanks a lot for this nice information

  2. eugener says:

    Your code for switch statements is incorrect. There is no need for break if you already returning a value.

  3. dcbyron says:

    You're right. What a silly mistake!

    I plead haste!

  4. Pingback: Some Java 7 features « Awe struck

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">