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* (可變)步驟時,編輯器會產生一個「非安全操作」的警告