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."







Nice article. want to see more.. thanks a lot for this nice information
Your code for switch statements is incorrect. There is no need for
breakif you already returning a value.You're right. What a silly mistake!
I plead haste!
Pingback: Some Java 7 features « Awe struck