分类分类
关注+2009-11-26作者:
Devoxx 大会结束在几天前结束了,一位与会者对此次大会的重要内容进行了总结,他提到Java 7的主要变化如下:
1.对collections的支持
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");
现在你还可以:
List<String> list = ["item"]; String item = list[0]; Set<String> set = {"item"}; Map<String, Integer> map = {"key" : 1}; int value = map["key"];
2.自动资源管理
BufferedReader br = new BufferedReader(new FileReader(path)); try { return br.readLine(); } finally { br.close(); }
becomes:
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.对通用实例创建(diamond)的type引用进行了改进
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
becomes:
Map<String, List<String>> anagrams = new HashMap<>();
4.数值可加下划线
int one_million = 1_000_000;
5.在switch中可使用string
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.二进制文字
相关文章
更多+相同厂商
热门推荐
点击查看更多
点击查看更多
点击查看更多
说两句网友评论