Java 7的主要变化http://code.joejag.com/2009/new-language-features-in-java-7/
来源:互联网 发布:linux查看jdk环境变量 编辑:程序博客网 时间:2024/06/08 17:24
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.二进制文字
- int binary = 0b1001_1001;
7.简化了可变参数方法的调用
当程序员试图使用一个不可具体化的可变参数并调用一个*varargs* (可变)方法时,编辑器会生成一个“非安全操作”的警告
Here are 7 of the new features that have been completed:
- Language support for collections
- Automatic Resource Management
- Improved Type Inference for Generic Instance Creation (diamond)
- Underscores in numeric literals
- Strings in switch
- Binary literals
- Simplified Varargs Method Invocation
There is a lot more to Java 7 then just these language changes. I’ll be exploring the rest of the release in future posts. One of the big debates is currently around Closures, which are a separate JSR.
Language support for collections
Java will be getting first class language support for creating collections. The style change means that collections can be created like they are in Ruby, Perl etc.
Instead of:
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");
You will be able to use:
List<String> list = ["item"];String item = list[0];Set<String> set = {"item"};Map<String, Integer> map = {"key" : 1};int value = map["key"];
These collections are immutable.
Automatic Resource Management
Some resources in Java need to be closed manually like InputStream, Writers, Sockets, Sql classes. This language feature allows the try statement itself to declare one of more resources. These resources are scoped to the try block and are closed automatically.
This:
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}
To support this behaviour all closable classes will be retro-fitted to implement a Closable interface.
Improved Type Inference for Generic Instance Creation (diamond)
This is a particular annoyance which is best served with an example:
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
becomes:
Map<String, List<String>> anagrams = new HashMap<>();
This is called the diamond operator: <> which infers the type from the reference declaration.
Underscores in numeric literals
Long numbers are hard to read. You can now split them up using an underscore in ints and longs:
int one_million = 1_000_000;
Strings in switch
Currently you can only use numbers or enums in switch statements. String has been added as a candidate:
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;}
Binary literals
Java code, due to its C heritage, has traditionally forced programmers to represent numbers in only decimal, octal, or hexadecimal.
As quite a few domains are bit orientated, this restriction can introduce errors. You can now create binary numbers using an 0b prefix.
int binary = 0b1001_1001;
Simplified Varargs Method Invocation
When a programmer tries to invoke a *varargs* (variable arity) method with a non-reifiable varargs type, the compiler currently generates an “unsafe operation” warning. JDK 7 moves the warning from the call site to the method declaration. This will enable API designers to use varargs due to the reduction of warnings reported.
- Java 7的主要变化http://code.joejag.com/2009/new-language-features-in-java-7/
- Java 7的主要变化
- Java 7的主要变化
- Java 7的主要变化
- Java 7.0 New Features
- 55 New Features in JDK 7 - NIO2
- Java SE 6 : New Features !
- Tiburon - new language features for Delphi 2009(有关泛型的)
- 300 new features, where's java 6?
- Web Tier to Go With Java EE 5: Summary of New Features in JSP 2.1 Technology
- Error:Jack is required to support java 8 language features. 及Jack的介绍
- Jack is required to support java 8 language features. Either enable Jack o的解决
- Error:Jack is required to support java 8 language features. 及Jack的介绍
- Java String Source code in openjdk-7
- Error:Jack is required to support Java 8 language features
- Error:Jack is required to support java 8 language features.
- 解决:Jack is required to support java 8 language features.
- Java —— Advanced Language Features —> static
- C#中ref和out的使用小结版本一
- Datagridview调整列宽
- flex 客户端分页
- HP笔记本CQ系列装XP无声音解决办法
- n个骰子的点数
- Java 7的主要变化http://code.joejag.com/2009/new-language-features-in-java-7/
- C#中ref和out的使用小结版本二
- 连接池
- 关于typeid —— typeid和RTTI C++
- 开始尝试着写博客
- 传智播客——系统相关类:Runtime,System
- 使用ASP.NET Global.asax 文件
- 我的书
- C#后台调用前台js方法