What’s new in Java 7

Generics in Collections
Automatic resource management
Enhancement to switch statement
Numeric literals with underscores
Multi-catch
Rethrowing exceptions with more inclusive type checking
NIO 2

Generics in Collections

With Java 7, when using Generics in Collections, you no longer have to specify the data type on both sides.
Prior Java 7: List<String> listOfStrings = new ArrayList<String>();
Java 7 and after: List<String> listOfStrings = new ArrayList<>();
Note:
– content assist for constructor invocations inserts a diamond instead of explicit type arguments
– compiler detects redundant specification of type arguments (can be removed via the Remove type arguments quick fix)

Automatic resource management

With this addition we get rid of two common problems occurring when using resources in Java:
– scope of the resource is to wide, since we need to declare our resource before the try statement
– we might forget to close used resources

Main feature of Try-with-resources is automatic resource closing. No longer we need to make sure we declare our resources before try statement and fear we forget to close it when writing the final statement.

In try statement we declare one or more resources. At the very end of the try statement, all resources will be closed automatically. All resources used in TWR must implement java.lang.AutoClosable.

You can read more about Exceptions and TWR here.

Enhancement to switch statement

A switch works with the byte, short, char, int (also wrapper classes Character, Byte, Short, and Integer) and enumerated types.
With Java 7, Switch Statements also support the use String class.

Numeric literals with underscores

This feature helps to separate groups of digits in numeric literals, which can improve the readability of the code.
This comes with some rules:
– we can place underscores only between digits
– we cannot place underscores in the following places: at the beginning or end of a number, adjacent to a decimal point in a floating point literal, prior to an F or L suffix and in positions where a string of digits is expected

Multi-catch

A single catch block can now handle more than one type of exception. This reduces code duplication and lessen the temptation to catch an overly broad exception.
Simply, just pipe the exceptions:
catch (IOException | SQLException ex) {…

Nice to know:
– we can observe a new quick fix in case there are multiple uncaught exceptions, Surround with try/multi-catch
– new quick assist options were added: Add exceptions to existing catch clause, Use separate catch blocks, Move exceptions to separate catch block,Combine catch blocks, Remove exception
– also, Mark Occurrences and Line Wrapping options were adjusted for this new feature

You can read more about Multi-catch here.

Re-throwing exceptions with more inclusive type checking

Prior to Java 7, we could throw only the exact exceptions that have the potential of being thrown in the method. Now we can throw an exception that is a supertype of one of the catch clause’s exception parameters.

Prior Java 7:
public void method() throws Exception {
try {
throw new SuperException();
} catch (Exception e) {
throw e;
}
Java 7 and after:
public void method() throws SuperException {
try {
throw new SuperException();
} catch (Exception e) {
throw e;
}

NIO 2

java.nio.file.Path interface
When referring to a file (reading, writing,..), file must exists. If Path object is used, we avoid exceptions when the file under this path doesn’t yet exists.

java.nio.file.Files class
This class offers variety of methods for File related operations. For more info, best to look here.

WatchService API
WatchService API provides a scalable solution for monitoring directories for changes.
StandardWatchEventKinds is a class whose instances tell the watch service the kinds of events to watch for on the registered directory:
StandardWatchEventKinds.ENTRY_CREATE
StandardWatchEventKinds.ENTRY_MODIFY
StandardWatchEventKinds.ENTRY_DELETE
StandardWatchEventKinds.OVERFLOW – triggered to indicate lost or discarded events

 

Last updated 11.07.2019, Sabina
For possible mistakes in content or typos, I apologise and kindly ask you to report here.