1.
对collections的支持
Java
代码
List<String> list = new ArrayList<String>();
list.add("item");
String item = list.get(0);
Set<String> set = new HashSet<String>();
set.add("item");
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("key", 1);
int value = map.get("key");
现在你还可以:
Java
代码
List<String> list = ["item"];
String item = list[0];
Set<String> set = {"item"};
Map<String, Integer> map = {"key" : 1};
int value = map["key"];
2.
自动资源管理
Java
代码
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
现在你还可以:
Java
代码
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
}
你可以定义更多的资源
try (
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest))
{
//
代码
}
3.
对通用实例创建(diamond)的type引用进行了改进
Java
代码
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
现在你还可以:
Java
代码
Map<String, List<String>> anagrams = new HashMap<>();
4.
数值可加下划线
Java
代码
int one_million = 1_000_000;
5.
在switch中可使用string
Java
代码
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 = 0b1001_1001;