Professional Documents
Culture Documents
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.
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…
This was a simplified example, maybe we should also check if the reader
isn’t already closed?
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.
case "PROD":
...
break;
case "QA":
...
break;
default:
...
break;
}
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.
// 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;
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.
// 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.
WRIT T EN BY
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