You are on page 1of 1

Become a member Sign in Get started

Best of: Java 7


Ben Weidig Follow
Oct 3, 2019 · 4 min read

This is the first post of a best of series showcasing the best/new features
of programming languages I use. The first one will be Java, starting with
version 7, even though Java 13 was just released a few days ago. But
much of the Java ecosystem still runs on older versions, and even if
you’re not, it’s nice to know about the new features that came with them.

This post was initially released on my blog: https://belief-driven-


design.com/2019-09-25/best-of-java-7/

Why Java
Java was never the quickest language in regards of new features
compared to newer languages, but IMHO this is one of the reasons why
most of the enterprise world still depends on it and will in the future. It’s
mostly compatible between versions, long-term-support is good, and
there are many options to run it on. Even though some of this changed
recently since Java 9 and the new release model…

We should always know the complete power of a language we’re using by


knowing all the new features and might adapt our coding style. Some
things are just syntactic sugar, but other features will help us to reduce
and avoid bugs, or get rid of an external-dependency, or even increase
performance with a simple code change.

Let’s check out the new features!

The try-with-resources statements


Dealing with I/O was quite cumbersome before Java 7. We have to make
sure that resources will be closed eventually, even if an exception occurs,
so we end up with a finally block:

BufferedReader reader = new BufferedReader(...);


try {
reader.readLine();
...
}
finally {
if (reader != null) {
reader.close();
}
}

This was a simplified example, maybe we should also check if the reader
isn’t already closed?

With Java 7 and the new try-with-resources statement we can streamline


this code and make it more readable!

try (BufferedReader reader = new BufferedReader) {


reader.readLine();
...
}

The magic in the background is the new interface java.lang.AutoClosable

which allows the automatic closing of resources at the end of the try-
with-resources block.

Multiple resources are also supported, they are close in reversed order
of declaration:

try (
BufferedReader reader = new BufferedReader(...);
BufferedWriter writer = new Files.newBufferedWriter(...)
) {
...
}
// After this block first 'writer', then 'reader' will be closed.

The try-with-resources statement is also a normal try -block, so feel free


to add catch or finally blocks, they are run after the resources are
closed.

Only caveat is exception handling… of course the closing of resources


can throw exceptions. But what happens if our code in the try block
throws an exception AND the closing of the resource throws another
one? In this case Java surpresses the closing exceptions, but they are
reachable via #getSurpressed() of the exception, curtosy of
java.lang.Throwable .

switch with String literals


Starting with Java 8 the switch statement is capable of handling String
literals for its case blocks:

String mode = ...


switch (mode) {
case "DEV":
...
break;

case "PROD":
...
break;

case "QA":
...
break;

default:
...
break;
}

The comparison is case-sensitive because String#equals is used. switch

statements generate more efficient bytecode compared to lots of chained


if-then-else .

Binary literal
I don’t work much with binary in my code, but it’s still a nice addition:
integral types ( byte , short , int , long ) are now as easily declarable as
hex-number:

// Hex
int asHex = 0x25 // = 35

// NEW: Binary
int asBinary = 0b100011 // = 35

We can either use 0b or 0B as prefix, and might left-pad the value with as
many zeros as needed.

The advantage here is that you can better visualize and compare binary
variables that might have a direct relationship to another, which wouldn’t
be visible in another form of representation.

Improved numeric literals


Numeric literals (including the new binary literal) can now be written in
a more comprehensible way, with underscores as separators:

// OLD
long oldCc = 4111111111111111l;
long oldHex = 0xAAFF23C522;
long oldBinary = 0b111000010111;

// NEW
long cc = 4111_1111_1111_1111l;
long hex = 0xAA_FF_23_C5_22;
long binary = 0b1110_0001_0111;

Some restrictions occur on the placement of the underscore though:

not at the beginning or end of a number

not adjacent to the decimal point

not adjacent to prefixes/suffixes (e.g. 0x, f)

Multiple Exceptions
Instead of multiple catch blocks we can now catch multiple exception
types in a single block. It’s nice to have as many different catch blocks as
we might need, but if handling them doesn’t differ we combine them with
the pipe symbol |:

// OLD
try {
...
}
catch (NullPointerException ex) {
...
}
catch (IllegalArgumentException ex) {
...
}

// NEW
try {
...
}
catch (NullPointerException | IllegalArgumentException ex) {
...
}

The generated bytecode won’t duplicate any code and will be smaller
and more efficient compared to multiple identical catch blocks.

Improved generic type inference for creation


Generics are great except typing so many types:

// OLD
Map<Long, List<String>> map = new HashMap<String, List<String>>();

With Java 7 we don’t need to specify the types on the creation part
thanks to the diamond operator: <> :

// NEW
Map<Long, List<String>> map = new HashMap<>();

Most likely your IDE is already removing redundant types, or at least you
should configure it this way to remove some noise from your code.

There’s more
Of course there’s more to Java 7 than just these few new language
features, here’s the overview. We should always thrive to embody
changes to our toolchain if they provide us with simpler patterns, new
features or better performance. Sometimes it might be even worth to
rewrite some code, but don’t force the new stuff on old code if not
needed.

Programming Java Jdk

WRIT T EN BY

Ben Weidig Follow

Software developer, entrepeneur, blogger. Mostly Java,


sometimes Swift, Golang, Bash and all the other fun stu .

Write the rst response

More From Medium

Quick and Simple —How A Beginner’s Guide to T he quirks of Ordering of events in


to Setup Jenkins Docker programming music Ka ka
Distributed (Master- Kishan Reddy in Better theory Siddhartha Bhattacharya
Slave) Build on Programming Nick Rose
Kubernetes
Kurnianto Trilaksono in T he
Startup

How to start making Zero bug policy. A fast Implementing Clean Don’t be a one trick
pixel art #1 way for paying back Architecture + MVVM pony
Pedro Medeiros in Pixel technical debt. Anthony Liberatore Alexander Curtis
Grimoire Roland Flemm in Serious
Scrum

Discover Medium Make Medium yours Become a member


Welcome to a place where words matter. On Medium, smart Follow all the topics you care about, and we’ll deliver the Get unlimited access to the best stories on Medium — and
voices and original ideas take center stage - with no ads in best stories for you to your homepage and inbox. Explore support writers while you’re at it. Just $5/month. Upgrade
sight. Watch

About Help Legal

You might also like