Tuesday, November 24, 2009
Nice Java 7 features
Some upcoming nice new features in Java 7
Underscores in numeric literals : int one_billion = 1_000_000_000; // Comma would have been more natural
Binary Literals : int binary = 0b1001;
Strings in switch statements :
String s = ...
switch(s) {
case "asinine":
processAsinine(s);
break;
case "foo":
case "bar":
processFooOrBar(s);
break;
// fall-through
default:
processDefault(s);
break;
}
Not necessary to close resources like InputStream, Writers, Sockets, Sql classes explicitly
This:
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
becomes
try (BufferedReader br = new BufferedReader(new FileReader(path)) { //copied from C# ?
return br.readLine();
}
Collections ( List,Set and Map) can be created like in Ruby or Perl. Instead of:
List<String> list = new ArrayList<String>();
list.add("myitem");
String item = list.get(0);
we can do:
List<String> list = ["myitem"];
String item = list[0];