J2SE
Version 7.0
Code named Dolphin and released on
July 28, 2011.
- Improved Type Inference for Generic Instance
Creation(Diamond operator)
- String in switch statements
- Automatic Resource management.
- Binary literals for ex: - int binary = 111000111;
- Underscore in literals for ex:- double payment_amount = 1_999_777_654.90;
- Multiple exception handling
- Support for Dynamic Languages
- Simplified Vararg method invocation: - This enhancement is not a new functionality, but only a move of a warning. As we know, we cannot create array of generics type because the type verification is not made at the same time. But with help of Java 7, we can made that type of array implicitly and the compiler generate warning at each invocation for those methods, so the warning has moved to the method declaration in order to have less warnings.
- A new interface AutoCloseable is introduced. Existing Closeable interface is changed to extend AutoCloseable interface
- A new method addSuppressed(Exception) is included to Throwable.
- ThreadLocalRandom: - A random number generator isolated to the current thread. ThreadLocalRandom is a simple addition to the Java SDK; it is very helpful when using random numbers in highly concurrent applications.
Examples of Some of JAVA 7 features:
-
Diamond Operator
If we want to declare a map of student using Generics, we will write the code as follows:
Map<String, List<Student>> studentsMap = new HashMap<String, List<Student>> ();
Here we have to declare the types on both the sides, although the right-hand side seems a bit redundant.
In Java 7, it’s written like this:
Map<String, List<Student>> trades = new HashMap <> ();
Using strings in switch statements
In Java 7, we can improve the program by utilizing the enhanced Switch statement, which takes a String type as an argument.
public void processTrade(Employee e) {
String status = stud.getJoiningStatus();
switch (status) {
case NEW:
newEmployeeFacility(e);
break;
case ONEYEAROLD:
executeBonus(e);
break;
case FIVEYEAROLD:
executeGratuityBonus(e);
break;
default:
break;
}
}
In thi program, here status field always uses String.equals() method to compare against the case label.
Automatic resource management
If we want to declare a map of student using Generics, we will write the code as follows:
Map<String, List<Student>> studentsMap = new HashMap<String, List<Student>> ();
Here we have to declare the types on both the sides, although the right-hand side seems a bit redundant.
In Java 7, it’s written like this:
Map<String, List<Student>> trades = new HashMap <> ();
Using strings in switch statements
In Java 7, we can improve the program by utilizing the enhanced Switch statement, which takes a String type as an argument.
public void processTrade(Employee e) {
String status = stud.getJoiningStatus();
switch (status) {
case NEW:
newEmployeeFacility(e);
break;
case ONEYEAROLD:
executeBonus(e);
break;
case FIVEYEAROLD:
executeGratuityBonus(e);
break;
default:
break;
}
}
In thi program, here status field always uses String.equals() method to compare against the case label.
Automatic resource management
Usually we are using a try-finally
block to close the respective resources like Connections, Files,
Input/OutStreams etc. That should be closed manually by the developer while
using. Java 7 has introduced another cool feature to manage the resources
automatically.
public void Java7TryExample() {
try (FileOutputStream fos = new FileOutputStream("employee.txt");
DataOutputStream dos = new DataOutputStream(fos)) {
dos.writeUTF("This is Gaurav and the employee id is - 32156");
} catch (IOException e) {
e.getMessage();
}
}
}
Here we don't have to nullify or
close the streams manually, as they are closed automatically once the control
exists from try block.
Note: - Any resource that implements AutoCloseble interface can be a used for automatic resource management. AutoCloseable interface is the parent of java.io.Closeable interface. It has only one method close() that would be called by the JVM when the control comes out of the try block.
Numeric literals with underscores
Using Java 7, we can use underscores
while defining numbers. For example, we can declare 1000 as below:
int indian_rupees = 1_000;
or 1000000 (one million) as follows
int million = 1_000_000
Improvements in exception handling
Multi-catch functionality is
introduced in java 7 to catch multiple exception types using a single catch
block.
public void Java7MultiCatchExample() {
try {
methodThatThrowsThreeExceptions();
} catch (Exception1 | Exception2 | Exception3 e) {
// log the Exceptions
}
}
No comments:
Post a Comment