1. Strings with switch statement:-
JDK7 allows you to use strings with switch statement. In this case
swicth internally uses equals method. This structure is always better that
if-else-if ladder. Here is one simple example,
String numbers = “one”;
Int x = 1000;
switch(numbers) {
case “one”: x =
1; break;
case “two”: x = 2;
break;
case “three”: x =
3; break;
case “four”: x =
4; break;
:
:
default :
System.out.print(“This is wrong option.”);
}
2. try-with-resources statement:-
In this try statement we can declare one or more resources
which must be closed after it’s work is finished. Here is one small example,
try(BufferedReader br = new BufferedReader(file)) {
br.readLine();
}
In this code br will be closed automatically in normal
execution as well as exceptional condition onse it is out of try block. Before
JDK7 we were using finally block. If exception is occuring then that exception
will be suppressed and that object will be closed. You can obtain that
suppressed exception using Throwable.getSuppressed method.
Again this feature introduces new interface called
AutoClosable.
Catching multiple exceptions:-
This is very simple to understand. JDK7 allows single catch
to handle multiple exceptions. This reduces code redundancy. Here is simple
exmaple,
try {
//Some
code.
} catch(IOException | SQLException e) {
}