7 new cool features in Java 7


1. Strings in switch Statements (doc)

Did you know previous to Java 7 you could only do a switch on char, byte, short, int, Character, Byte, Short, Integer, or an enum type (spec)? Java 7 adds Strings making the switch instruction much friendlier to String inputs. The alternative before was to do with with if/else if/else statements paired with a bunch of String.equals() calls. The result is much cleaner and compact code.

public void testStringSwitch(String direction) {
switch (direction) {
case “up”:
y–;
break;

case “down”:
y++;
break;

case “left”:
x–;
break;

case “right”:
x++;
break;

default:
System.out.println(“Invalid direction!”);
break;
}
}

———-

2. Type Inference for Generic Instance Creation (doc)
Previously when using generics you had to specify the type twice, in the declaration and the constructor;

view plaincopy to clipboardprint?
List strings = new ArrayList();
In Java 7, you just use the diamond operator without the type;

view plaincopy to clipboardprint?
List strings = new ArrayList();
If the compiler can infer the type arguments from the context, it does all the work for you. Note that you have always been able do a “new ArrayList()” without the type, but this results in an unchecked conversion warning.

Type inference becomes even more useful for more complex cases;

view plaincopy to clipboardprint?
// Pre-Java 7
// Map<String,Map>m=new HashMap<String, Map>();

// Java 7
Map<String, Map> m = new HashMap();

————

3. Multiple Exception Handling Syntax (doc)

Tired of repetitive error handling code in “exception happy” APIs like java.io and java.lang.reflect?

try {
Class a = Class.forName(“wrongClassName”);
Object instance = a.newInstance();
} catch (ClassNotFoundException ex) {
System.out.println(“Failed to create instance”);
} catch (IllegalAccessException ex) {
System.out.println(“Failed to create instance”);
} catch (InstantiationException ex) {
System.out.println(“Failed to create instance”);
}

When the exception handling is basically the same, the improved catch operator now supports multiple exceptions in a single statement separated by “|”.

try {
Class a = Class.forName(“wrongClassName”);
Object instance = a.newInstance();
} catch (ClassNotFoundException | IllegalAccessException |
InstantiationException ex) {
System.out.println(“Failed to create instance”);
}

Sometimes developers use a “catch (Exception ex) to achieve a similar result, but that’s a dangerous idea because it makes code catch exceptions it can’t handle and instead should bubble up (IllegalArgumentException, OutOfMemoryError, etc.).
————

4. The try-with-resources Statement (doc)

The new try statement allows opening up a “resource” in a try block and automatically closing the resource when the block is done.

For example, in this piece of code we open a file and print line by line to stdout, but pay close attention to the finally block;

try {
in = new BufferedReader(new FileReader(“test.txt”));

String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (in != null) in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

When using a resource that has to be closed, a finally block is needed to make sure the clean up code is executed even if there are exceptions thrown back (in this example we catch IOException but if we didn’t, finally would still be executed). The new try-with-resources statement allows us to automatically close these resources in a more compact set of code;

try (BufferedReader in=new BufferedReader(new FileReader(“test.txt”)))
{
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}

So “in” will be closed automatically at the end of the try block because it implements an interface called java.lang.AutoCloseable. An additional benefit is we don’t have to call the awkward IOException on close(), and what this statement does is “suppress” the exception for us (although there is a mechanism to get that exception if needed, Throwable.getSuppressed()).
————–

5. Improved File IO API (docs 1, 2)

There are quite a bit of changes in the java.nio package. Many are geared towards performance improvements, but long awaited enhancements over java.io (specially java.io.File) have finally materialized in a new package called java.nio.file.

For example, to read a small file and print all the lines (see example above);

List lines = Files.readAllLines(
FileSystems.getDefault().getPath(“test.txt”), StandardCharsets.UTF_8);

for (String line : lines) System.out.println(line);
java.nio.file.Path is an interface that pretty much serves as a replacement for java.io.File, we need a java.nio.file.FileSystem to get paths, which you can get by using the java.nio.file.FileSystems factory (getDefault() gives you the default file system).

java.nio.file.Files then provides static methods for file related operations. In this example we can read a whole file much more easily by using readAllLines(). This class also has methods to create symbolic links, which was impossible to do pre-Java 7. Another feature long overdue is the ability to set file permissions for POSIX compliant file systems via the Files.setPosixFilePermissions method. These are all long over due file related operations, impossible without JNI methods or System.exec() hacks.

I didn’t have time to play with it but this package also contains a very interesting capability via the WatchService API which allows notification of file changes. You can for example, register directories you want to watch and get notified when a file is added, removed or updated. Before, this required manually polling the directories, which is not fun code to write.

For more on monitoring changes read this tutorial from Oracle.
————————

6. Support for Non-Java Languages: invokedynamic (doc)

The first new instruction since Java 1.0 was released and introduced in this version is called invokedynamic. Most developers will never interact or be aware of this new bytecode. The exciting part of this feature is that it improves support for compiling programs that use dynamic typing. Java is statically typed (which means you know the type of a variable at compile time) and dynamically typed languages (like Ruby, bash scripts, etc.) need this instruction to support these type of variables.

The JVM already supports many types of non-Java languages, but this instruction makes the JVM more language independent, which is good news for people who would like to implement components in different languages and/or want to inter-operate between those languages and standard Java programs.
—————————-
7. JLayerPane (doc)

Finally, since I’m a UI guy, I want to mention JLayerPane. This component is similar to the one provided in the JXLayer project. I’ve used JXLayer many times in the past in order to add effects on top of Swing components. Similarly, JLayerPane allows you to decorate a Swing component by drawing on top of it and respond to events without modifying the original component.

For example a component using this functionality, providing a “spotlight” effect on a JLayerpanel.

You could also blur the entire window, draw animations on top of components, or create transition effects.