Recently released Java 7 has a lot of useful API enhancements such as the automatic resource management, multi-catch
statements, switch statement with strings, binary literals and improved numeric literals. This post is a quick roundup
of the new features, which should help you to get a full picture of Java 7 syntax in a short time.
Automatic Resource Management
The try-with-resources statement is a 'try' that accepts one or more resources which will be closed automatically when
the statement completes. Resources must implement the AutoCloseable or Closeable interface to be used with the
try-with-resources block:
12345678910111213
classResourceimplementsAutoCloseable{publicvoiddoAction()throwsException{// throws the exception when doAction() is being calledthrownewException("Resource action exception");}@Overridepublicvoidclose()throwsException{// throws the exception when the resource is being closedthrownewException("Resource close exception");}}
1234567891011
try(Resourceresource=newResource()){// the resource will be automatically closed when doAction()// throws the exceptionresource.doAction();}catch(Exceptione){// catches the exception thrown by the doAction() method,// the exception thrown by the close() method is suppressed,// but still can be retrieved with Throwable.getSuppressed()e.getSuppressed()[0]// returns the "Resource close exception"throwe;// throws the "Resource action exception"}
Catching multiple exception types
A single catch block can handle several different exception types, which was impossible in prior Java versions. For
example, the try-catch:
12345
catch(ConnectExceptionex){thrownewAPIException("Cannot connect to the database");catch(DBConnectExceptionex){thrownewAPIException("Cannot connect to the database");}
can be replaced with:
1234
catch(ConnectException|DBConnectExceptionex){// handles both the ConnectException and the DBConnectExceptionthrownewAPIException("Cannot connect to the database");}
Strings in switch statements
Java 7 finally allows to use a String object in the expression of a switch statement. The new switch compares strings
using the String.equals() method:
The switch statement, of course, is much more preferable choice, as the code generated by a compiler will be more
efficient.
Binary literals
New binary literals must be prefixed with 0b or 0B, for example:
123
inti1=0b101;// orinti2=0B101;
You can also use binary literals to express integral types:
1234
byteb=0b10010010// up to 8 bitshorts=0b1001001010010010// up to 16 bitinti=0b10010010100100101001001010010010// up to 32 bitlongl=0b1001001010010010100100101001001010010010100100101001001010010010//up to 64 bit
Binary literals are very convenient in bitwise and bitshift operations:
Underscores can be used to visually separate digits and make code more readable, for example, binary or hexadecimal
literals can be separated by bytes:
These are almost all syntax changes in Java 7, and yes, it would be nice to have more syntactic sugar. The good news
is the Java 8 release is coming soon, which, with lambda expressions and method references, promises to have a lot of
extremely interesting features and improvements.
If you're looking for a developer or considering starting a new project,
we are always ready to help!