The Devoxx conference ended a few days ago. An attendee summarized the important contents of this conference. He talked about the important changes in Java 7 as follows:
1. Support for collections
Java code 1.List list = new ArrayList();
2.list.add("item");
3.String item = list.get(zero);
4.
5.Set set = new HashSet();
6.set.add("item");
7.
8.Map map = new HashMap();
9.map.put("key", 1);
10.int value = map.get("key");
List list = new ArrayList();
list.add("item");
String item = list.get(zero);
Set set = new HashSet();
set.add("item");
Map map = new HashMap();
map.put("key", one);
int value = map.get("key"); Now you can still:
Java code 1.List list = ["item"];
2. String item = list[zero];
3.
4.Set set = {"item"};
5.
6.Map map = {"key" : one};
7.int value = map["key"];
List list = ["item"];
String item = list[zero];
Set set = {"item"};
Map map = {"key" : one};
int value = map["key"];
2. Automatic resource management
Java code 1.BufferedReader br = new BufferedReader(new FileReader(path));
2.try {
3. return br.readLine();
4.} finally {
5. br.close();
6.}
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
becomes:
Java code 1.try (BufferedReader br = new BufferedReader(new FileReader(path)) {
2. return br.readLine();
3.}
4.
5. You can declare more than one resource to close:
6.
7.try (
8. InputStream in = new FileInputStream(src);
9. OutputStream out = new FileOutputStream(dest))
10.{
11. // code
12.}
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
}
You can declare more than one resource to close:
try (
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest))
{
// code
}
3. Improved the type excerpt of general example creation (diamond)
Java code 1.Map> anagrams = new HashMap>();
Map> anagrams = new HashMap>();
becomes:
Java code 1.Map> anagrams = new HashMap<>();
Map> anagrams = new HashMap<>(); 4. Values can be underlined
Java code one.int one_million = one_000_000;
int one_million = one_000_000;
5. String can be used in switch
Java code 1.String s = ...
2.switch(s) {
3. case "quux":
4. processQuux(s);
5. // fall-through
6.
7. case "foo":
8. case "bar":
9. processFooOrBar(s);
10. break;
11.
12. case "baz":
13. processBaz(s);
14. // fall-through
15.
16.default:
17. processDefault(s);
18.break;
19.}
String s = ...
switch(s) {
case "quux":
processQuux(s);
// fall-through
case "foo":
case "bar":
processFooOrBar(s);
break;
case "baz":
processBaz(s);
// fall-through
default:
processDefault(s);
break;
}
6. Binary literals
Java code one.int binary = zero b1001_1001;
int binary = zero b1001_1001;
7. Simplified the calling of variable parameter steps
The editor generates an "unsafe operation" warning when the programmer intends to use a non-reducible varargs and calls a *varargs* (variable) step.