} catch(IOException |
SQLException e) {
//rethrow or logging
}
|
2. Numeric literals with underscores:
To prevent eye misdirection in numbers with multiple zeros, you can declare these types of numbers as below:
int thousand = 1_000;
System.out.println(thousand);
|
In Java6:
3. Diamond Operator:
You don't have to declare types in both sides:
List<String>
list1 = new ArrayList<>();
|
In Java 6:
4. Automatic resource management:
Connections, Files, Input/OutStreams etc. are auto closed. (java.lang.AutoCloseable).
Connections, Files, Input/OutStreams etc. are auto closed. (java.lang.AutoCloseable).
public void newTry() {
try(FileOutputStream fos = new FileOutputStream("test.txt");
DataOutputStream dos = new
DataOutputStream(fos)) {
dos.writeUTF("Java 7
Automatic resource management");
} catch (IOException e) {
// log the exception
}
}
|
In Java 6:
public void oldTry() {
FileOutputStream fos = null;
DataOutputStream dos = null;
try {
fos = new FileOutputStream("test.txt");
dos = new
DataOutputStream(fos);
dos.writeUTF("Java 7
Automatic resource management");
} catch (IOException e) {
// log the
exception
} finally {
try {
fos.close();
dos.close();
}
catch (IOException e) {
// log the
exception
}
}
}
|
No comments:
Post a Comment