JDK 1.7 Features

JDK 1.7 Features


JDK 1.7 (Java SE 7 or Java 7), code named as Dolphin was released on July 28, 2011.

As on today (Sept 1, 2013) the latest update 25 of Java 7 was released on June 18, 2013.

Following is the list JDK 1.7 Features added, of daily importance.

1. String in Switch Expression
2. Underscores Between Digits in Numeric Literals
3. Integral Types as Binary Literals
4. Handling multiple exceptions in a single catch block
5. Try-with-resources Statement
6. Automatic Type Inference in Generic object instantiation

1. String in Switch Expression

Earlier to JDK 1.7, switch expression takes int values or convertible to int. From JDK 1.7, switch accepts string objects also as expression.

JDK 1.7 Features The switch can be used also as follows while accessing command-line arguments.

JDK 1.7 Features Switch does case-sensitive comparison with case statements. Switch gives a more efficient and cleaner code than if-else if-else code.

2. Underscores Between Digits in Numeric Literals

Underscores are permitted in numeric literals. You can place underscores where you feel required to increase readability; like between hundreds and thousands, thousands and lakhs etc.

This is used to group numbers in a bigger literal value (especially of long data type).

Note: Do not place underscore at the beginning or ending of the literal value.

JDK 1.7 Features
Note: But following does not work (cannot parse with underscores).

The above parsing raises NumberFormatException.

3. Integral Types as Binary Literals

With JDK 1.7, the integer whole numbers like byte, short, int and long can be expressed in binary format also with a prefix of 0b or 0B. Earlier, we have 0 prefix for octal and 0x prefix for hexa and no prefix for binary. JDK 1.7, introduced 0b to represent binary literals.

JDK 1.7 Features
The mango cost statement can be written as follows also with underscores.

int mangoCost = 0b001_110_11;

4. Handling multiple exceptions in a single catch block

Before JDK 1.7, it is required to write multiple catches to handle different exceptions raised in the code. Now in a single catch statement, multiple exceptions can be included separated by pipe ( | ) symbol.

Following is the earlier to JDK 1.7 code which you are well familiar with.

JDK 1.7 Features

The above multiple catches can be replaced with single catch as follows in JDK 1.7.

JDK 1.7 Features

Observe the pipe symbol.

catch(ArithmeticException | ArrayIndexOutOfBoundsException e)

Note: Following does not work:

JDK 1.7 Features

Super class exception must be caught separately (it is a constraint).

5. Try-with-resources Statement
(try statement defining resources)

With JDK 1.7, no finally block is required to close (with close() methods) the resources of files or sockets or JDBC handles (objects) etc. The resources (say objects) opened in try block automatically close when the execution control passes out try block (say, at the close brace of try block).

Your Earlier code:

Your Present code with JDK 1.7:

A new interface AutoCloseable is introduced with JDK 1.7 for this purpose.

Any resource (class) that implements interface "java.lang.AutoCloseable" is eligible as a resource statement to write in try block. From JDK 1.7, many classes implement AutoCloseable interface, like BufferedReader, PrintStream, Scanner, Socket etc.

The close() method of AutoCloseable is called implicitly to close the handles. Observe, the close() method throws Exception which you may or may not catch if your code does not demand, or replace with problem specific exception class. In the above code, I used IOException. Note that close() method java.lang.Closeable interface is very different from this.

JDBC 4.1 can make use of this try-catch-resource management. Following is the snippet of code.

6. Automatic Type Inference in Generic object instantiation
(Diamond operator, <> , in collection classes)

In JDK 1.7, empty angle brackets (known as diamond operator), <>, can be used in specifying generic type instead of writing the exact one. But remember, the compiler should be able to judge the type from the generics statement you write.

Other improvements are in the fields of java.nio, Phasers and TransferQueue in Concurrency API, Pools and Tasks in Fork Join Framework, java.util.ThreadLocalRandom, ClassLoader Improvements – Deadlock Avoidance, URLClassLoader Improvements, Unicode 6.0 to include thousands of new characters to support Japanese Telcos and Indic scripts, Extensible Currency Codes, NumericShaper for number shaper enhancements, Locale enhancement – Categories, Nimbus Look and Feel for Swing components (better than earlier Metal), Standardize JLayer Component for easy enriching Swing components, Standardize Translucent Windows etc. I have not discussed the above as they are are not for daily usage in coding.

7. Static blocks

Earlier to JDK 1.7, to print static blocks no main() method is required. But from JDK 1.7, if no main() exists, static blocks will not be executed.

Would like to see JDK 1.8 Features also?

Refer for all Java Versions.

Following links gets you JDK 1.7 version download

1. http://java.com/en/download/index.jsp

2. http://www.herongyang.com/JDK/JDK-170-Windows-Download-Installation.html

For different miscellaneous topics on Java, refer View All Java Examples

12 thoughts on “JDK 1.7 Features

Leave a Reply

Your email address will not be published. Required fields are marked *