Sep 20, 2011

Java v7 Released

Released on July 28, Java 7, now from Oracle, brings a few improvements in the language that are worth noting. The changes aren’t as big as the changes introduced in Java 5, which included generics, annotations, varargs, auto-boxing, and enumerations, just to name a few. The new features in Java 7 aren’t as likely to cause upgrade problems, either, as the earlier JDK did by introducing keywords that many people used (e.g., “enum”). A couple of new language bits that might come in handy include String support in switch statements, try-with-resources, and catching multiple exceptions. A few nice things that might cause some head-scratching (until everyone catches on) include binary and underscored literals, and the diamond operator used for declaring typed objects.

Here’s a few more details on some of the highlights.

Binary literals offer a way to describe a number in its binary form. As a literal value of ten would be marked as a decimal value”10″ and could be expressed in octal (012) or hexadecimal (0x0a), now there’s a binary option (0b1010) as well. Only integral values are allowed, as in not a float; so the types byte, char, int, and long. This strength will probably be used more in applications that use masks; that is, if you want the last four bits in a number, you can use the binary literal, instead of figuring out the value is 15:

int masked = (int)raw & 0b1111;

Underscored literal numbers are a visual clue for developers more than a useful thing for the compiler. What this allows is breaking apart large numbers into readable chunks. Common uses might include breaking large numbers into “thousands” (1_000), or patterned numbers (that should probably be strings, but maybe are numbers to ensure content) such as credit cards (1234_5678_9012_3456) or social security numbers (123_45_6789). The underscores can be multiple (1__2) if desired. The only place they can’t be used are at the beginning (_1) or end (2_) of the number, including prefixes (0_x0 or (0x_0) or suffixes (4_L), or next to a decimal point (3_.14 or 3._14); all of which kind of boil down to not at the beginning or end of a number.

Type inference, via diamond operators, are used to shortcut generic use in variable definitions. For example, a List of Strings might be declared as List<String> list = new LinkedList<String>() in Java 6, but in Java 7 we can shortcut that to List<String>list = new LinkedList<>() instead. This avoids unchecked conversion warnings, while reducing code typing (and reading) time. There are limitations on when this can be used, which are dependent on how obvious the use of the generic is on the constructor of the type, so this is one that might not be implemented as often. It will possibly take over for some @SuppressWarnings where the definitions aren’t provided.

The String switch statement is likely to be the most popular language addition. It will take the place of many if/else constructs, finally allowing a simple switch(string){"value":break;"other":break;} constriction. This will eliminate a lot of cases where strings are turned into other kinds of values for this purpose. It will also shorten a lot of code where strings prevail.

Another new feature that will probably become quickly popular is the try-with-resources statement. At the first glance, this is an easy spot to reduce code for classes that offer or require closing after using. Easy examples include readers and writers. A new interface is used to help identify such resources, and any object which implements the AutoCloseable interface will work. One or more objects can be declared in parenthesis after the try statement, and when the try ends, the object is closed. This works whether the try ends in success or failure. This reduces the need to add finally statements just to try to close the resources.

try(FileWriter fileWriter = new FileWriter(File.createTempFile("foo","tmp"))){
fileWriter.write("Hello, world!",0,13);

The example creates a temporary file, writes a string to it, and then closes the file. All of this is done in these two lines. The scope of the fileWriter variable is limited to within the try block, as if declared inside the braces.

At the other end of the try-catch block, Java 7 now supports multiple exceptions in one catch declaration. This will help reduce blocks of code where perhaps the same (or no) actions occur for different exceptions.

try{ /* Something */ } catch (IOException|NullPointerException e){}

Additional improvements include new classes added to Swing, concurrency, I/O, and collections. Updates for RIA in applets, XML processing, and JDBC are also present. New garbage collection and other enhancements have also been made to the JVM specifications.

A full list of the items is available at with a bit more discussion at the reference implementation the preceded the Oracle release

One downside I did find while toying with the new JDK comes with tools that affect the compiled class files. After struggling with it for a bit, it seemed reasonable, but at the time, I thought that the new JDK was somehow to blame. Using Eclipse, I added the new JDK to my workspace, and changed the default compiler. After recompiling the code with no failures, I attempted to run JUnit tests with code-coverage reporting via the EclEmma plug-in.

The failures reported were because of the way Emma instruments the code. Running the JUnit without the code-coverage ran as expected, and everything was fine. At this writing, neither Emma or Cobetura have been updated to work with Java 7, so projects using either code-coverage tool will fail under Java 7.

About the Author

Object Partners profile.

One thought on “Java v7 Released

  1. Nick Adelman says:

    Unfortunately, some of the more intriguing features that had been discussed for JDK7 were not included. These include project jigsaw (slated for JDK8) and closures (slated for JDK ???), among others.

  2. Jeff Warren says:

    I agree, they missed out on some important things in their effort to get something out there. I guess as with any software release cycle, somethings just aren’t done enough to be included.

    There were more changes than I covered. Most of those had to do with new or changed classes or packages, including Swing, I/O, Networking, Security, and Concurrency. I wanted to touch on the language changes, as those seem to be the first things to impact most people. Putting some of the XML processing (JAX-P, JAX-B, and JAX-WS) right in the JDK is a nice move, removing additional, and nearly ubiquitous implementations.

    While Java v7 itself doesn’t address closures directly, the v7 JVM does contain support for “dynamic languages” which seems like a step in the right direction. (

    And it seems they made the JDK bigger with the additional packages and classes, making the promise of jigsaw seem more intriguing. It’ll be interesting to see if they can neatly split it up. Most of us don’t really need Swing classes in our web apps, right?

Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]