Tags

, , , , , ,

Here we see some features and enhancements made in Java 7. Click the below links to see Java 7 features I described in previous posts.

Java 7 features and enhancements
File notifcation using Java 7

Automatic Resource Management

Resources such as Connections, Files, Input/OutStreams, etc. should be closed manually by the developer by writing bog-standard code. Usually we use a try-finally block to close the respective resources. Below is the current practice of creating a resource, using it and finally closing it:

public void oldMethod() {
try {
fos = new FileOutputStream(“test.txt”);
dos = new DataOutputStream(fos);
dos.writeUTF(“Java 7 features “);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
dos.close();
} catch (IOException e) {
// log the exception
}
}
}

Java 7 has introduced another nice feature to manage the resources automatically. All we have to do is declare the resources in the try as follows:
try(resources_to_be_cleant){
// your code
}

The above method with the old try can be re-written using this new feature as shown below:

public void newMethod() {
try (FileOutputStream fos = newFileOutputStream(“test.txt”);
DataOutputStream dos = newDataOutputStream(fos)) {
dos.writeUTF(“Java 7 features”);
} catch (IOException e) {
// log the exception
}
}

The above code also represents another aspect of this feature: working with multiple resources. The FileOutputStream and DataOutputStream resources are enclosed in the try statement one after the other, each one separated by a semicolon (;) separator. We do not have to nullify or close the streams manually, as they are closed automatically once the control exists the try block.
Behind the scenes, the resources that should be auto closed must implement java.lang.AutoCloseable interface.
Any resource that implements AutoCloseble interface can be a eligible for automatic resource management. The AutoCloseable is the parent of java.io.Closeable interface and has just one method close() that would be called by the JVM when the control comes out of the try block.

Exception Handling

There are a couple of improvements in the exception handling area. Java 7 introduced multi-catch functionality to catch multiple exception types using a single catch block. Let’s say you have a method that throws three exceptions. In the current state, you would deal them individually as shown in below:

public void oldMultiCatch() {
try {
//execute statements
} catch (ExceptionOne e) {
// log and deal with ExceptionOne
} catch (ExceptionTwo e) {
// log and deal with ExceptionTwo
} catch (ExceptionThree e) {
// log and deal with ExceptionThree
}
}

Catching an endless number of exceptions one after the other in a catch block looks cluttered. There are situations to have your code that catches a dozen exceptions, This is incredibly inefficient and error prone. Java 7 has brought in a new language change to address this ugly duckling. See the improved version of the method oldMultiCatch method below:

public void newMultiCatch() {
try {
//execute statements

} catch (ExceptionOne | ExceptionTwo | ExceptionThree e) {
// log and deal with all Exceptions
}
}

The multiple exceptions are caught in one catch block by using a ‘|’ operator. This way, you do not have to write dozens of exception catches. However, if you have bunch of exceptions that belong to different types, then you could use “multi multi-catch” blocks too. The following snippet illustrates this:

public void newMultiMultiCatch() {
try {
//execute statements

} catch (ExceptionOne e) {
// log and deal with ExceptionOne
} catch (ExceptionTwo | ExceptionThree e) {
// log and deal with ExceptionTwo and ExceptionThree
}
}

In the above case, the ExceptionTwo and ExceptionThree belong to a different hierarchy, so you would want to handle them differently but with a single catch block.

Numeric literals with underscores

The other feature is to simplify when you enter numeric literals with many zeros. It is difficult when a number has say, ten zeros. It’s quite error prone and cumbersome to identify a literal if it’s a million or a billion unless you count the places from right to left. Java 7 introduced underscores in identifying the places. For example, you can declare 1000 as shown below:

int thousand = 1_000;
or 1000000 (one million) as follows
int million = 1_000_000

Note that binary literals are also introduced in this release too — for example “0b1″ — so developers don’t have to convert them to hexadecimals any more.

 

Advertisements