Tuesday, February 3, 2015

Java 7 VS Java 6

1) Numeric  literals with underscores 

int i =100;
int i=100000;

Java 7:
int i = 1_00;
int i = 1_000_00;

2) Bracket notation for collections :

Collection <String>coll = new ArrayList<String>();
coll.add("kiran");
coll.add("kiran1");

Java 7:
Collection <String>coll = new ArrayList{"kiran","kiran1"};

3) try with resources :

BufferedReader reader = null;
try{
reader  =new BufferedReader( ...) ;

}catch(IOException exception) {
...
}finally {
try {
   reader .close();
}catch(IOException e) {
}
}

Java 7:
Ex 1:
 try (BufferedReader br = new BufferedReader(new FileReader(path)); 
BufferedWriter bw = new BufferedWriter(new FileWriter(path)); )
{ //File code
 } catch(IOException ioEx) {
} With Java7 try-with-resource the resources BufferedReader & BufferedWriter implements java.lang. AutoCloseable and will be closed automatically
Ex 2:
try (Statement stmt = con.createStatement())
 { ResultSet rs = stmt.executeQuery(query); while (rs.next()) String coffeeName = rs.getString("Name”); } catch (SQLException e) {
} With Java7 try-with-resource the resource javax.sql. Statement implements java.lang.AutoCloseable and will be closed automatically

4) Multiple Catch statement :

try{
..
}catch(Exception 1) {
}catch(Exception 1) {
}

Java 7:
try{
}catch(Exception 1 | Exception 2 | Exception 3) {
}
Clean code with multiple catch statements in one catch block.

5) Switch with string case

Previous java is not allowed string in switch case


java 7
switch(day) {
 case "monday" :...
}

6) Simple Generic

Simple Generic Instance Old-Way List<String> strings = new ArrayList<String>(); Map<String, List<String>> myMap = new HashMap<String, List<String>>(); New-Way (Simple) List<String> strings = new ArrayList<>(); Map<String, List<String>> myMap = new HashMap<>();

This is not a final list yet to write few more...

No comments: