Devoxx 大会完事在几天前完了了,一位到会者对本次大会的重要内容进行了小结,他谈到Java 七的重要变更如次:
一.对collections的支持
Java代码
一.List list = new ArrayList();
二.list.add("item");
三.String item = list.get(零);
4.
五.Set set = new HashSet();
六.set.add("item");
7.
八.Map map = new HashMap();
九.map.put("key", 一);
十.int value = map.get("key");
List list = new ArrayList();
list.add("item");
String item = list.get(零);
Set set = new HashSet();
set.add("item");
Map map = new HashMap();
map.put("key", 一);
int value = map.get("key"); 现下你还可以:
Java代码
一.List list = ["item"];
二.String item = list[零];
3.
四.Set set = {"item"};
5.
六.Map map = {"key" : 一};
七.int value = map["key"];
List list = ["item"];
String item = list[零];
Set set = {"item"};
Map map = {"key" : 一};
int value = map["key"];
二.自动资源管理
Java代码
一.BufferedReader br = new BufferedReader(new FileReader(path));
二.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代码
一.try (BufferedReader br = new BufferedReader(new FileReader(path)) {
2. return br.readLine();
3.}
4.
五.You can declare more than one resource to close:
6.
七.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
}
三.对通用范例创造(diamond)的type摘引进行了改善
Java代码
一.Map> anagrams = new HashMap>();
Map> anagrams = new HashMap>();
becomes:
Java代码
一.Map> anagrams = new HashMap<>();
Map> anagrams = new HashMap<>();四.数值可加下划线
Java代码
一.int one_million = 一_000_000;
int one_million = 一_000_000;
五.在switch中可运用string
Java代码
一.String s = ...
二.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.二进制文字
Java代码
一.int binary = 零b1001_1001;
int binary = 零b1001_1001;
七.简单化了可变参数步骤的调用
应程序员意欲施用一个不可细化的可变参数并调用一个*varargs* (可变)步骤时,编辑器会生成一个“非安全操作”的警告