New features in Java 7
Java 7 comes with lot of new exciting features which helps in increasing code readability and functionality.
Some of the important changes are listed below:-
- Collections-Generics-Diamond operator
- Automatic resource management
- Try-Catch block
- Switch statement -Strings etc.
1.Collections-Generics
In older versions of Java we used to declare generics on both sides of collection type creation like
In older versions of Java we used to declare generics on both sides of collection type creation like
List <String> ls=new ArrayList <String> ();
In the above declaration there is no need to define generics on right hand side.
Java 7 the compiler will infer the types from the left hand type declaration. automatically
There are two ways to declare them one is to completely remove the right hand side generics
Java 7 the compiler will infer the types from the left hand type declaration. automatically
There are two ways to declare them one is to completely remove the right hand side generics
List<String> ls=new ArrayList();
The above declaration will give us a warning.
The above declaration will give us a warning.
The other is using empty diamond operator.This will not provide any warning
List<String> ls=new ArrayList<>();
2.Automatic resource management
Resource management like freeing up resources is done by using finally block.
From Java 7 there is no need to explicitly define finally block, whenever execution finishes try block automatically resources declared in try block are freed.This feature effectively handles memory leaks though the developer forgets to write the code in finally block.
Example:-
public class ResourceHandling_Old {
/**
* @param args
*/
public static void main(String[] args) {
FileInputStream fis=null;
try
{
fis=new FileInputStream("C:\\Documents and Settings\\ignore.txt");
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In the above example developer is explicitly closing the FileInputStream(FIS) from Java 7 there is no need to write finally block.
Example:-
public static void main(String[] args) {
FileInputStream fis=null;
try
{
fis=new FileInputStream("C:\\Documents and Settings\\ignore.txt");
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
When the execution of try block finished the resources de allocation is automatically managed by JVM
3.Try-Catch block
See the below code how we handle multiple exceptions in Java.
try
{
fis=new FileInputStream("C:\\Documents and Settings\\ignore.txt");
fis.read();
stm= conn.createStatement();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
As we can see here there are 3 catch blocks to handle multiple exceptions.They may increase in number also.This type of handling catch block is quite cumbersome and decreases readability of the code.
Java 7 comes with new ways of handling it.
catch (IOException | SQLException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Note:- Neglected FileNotFoundException e because it is a part of IO exception.
The best way to declare this is
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException | SQLException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here the exceptions are handled in one line using | operator.
3.Try-Catch block
See the below code how we handle multiple exceptions in Java.
try
{
fis=new FileInputStream("C:\\Documents and Settings\\ignore.txt");
fis.read();
stm= conn.createStatement();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
As we can see here there are 3 catch blocks to handle multiple exceptions.They may increase in number also.This type of handling catch block is quite cumbersome and decreases readability of the code.
Java 7 comes with new ways of handling it.
catch (IOException | SQLException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Note:- Neglected FileNotFoundException e because it is a part of IO exception.
The best way to declare this is
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException | SQLException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here the exceptions are handled in one line using | operator.
4.Switch statements-Strings block
Earlier to Java 7 switch does not allow String variables as arguments..
Example :-
String countryName="India";
switch(countryName)
{
case "India":
System.out.println("Second largest populated country");
case "China":
System.out.println("First largest populated country");
}
There are lot more developments happened for more information refer
http://radar.oreilly.com/2011/09/java7-features.html
http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html
http://www.eclipse.org/jdt/ui/r3_8/Java7news/whats-new-java-7.html#try-with-resources
Earlier to Java 7 switch does not allow String variables as arguments..
Example :-
String countryName="India";
switch(countryName)
{
case "India":
System.out.println("Second largest populated country");
case "China":
System.out.println("First largest populated country");
}
There are lot more developments happened for more information refer
http://radar.oreilly.com/2011/09/java7-features.html
http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html
http://www.eclipse.org/jdt/ui/r3_8/Java7news/whats-new-java-7.html#try-with-resources
No comments:
Post a Comment