[51cto quick translation] Java 7 will include closures at the latest computing Conference xx.
Many people are excited-this will be implemented as an independent JSR. In numerous
Some of the new Java 7 language features are now complete. A participant at the computing Conference xx
The following seven new functions of Java 7 are reported:
1) language support for the Collection class;
2)
Automatic resource management;
3) improved general instance creation type inference;
4) supports literal underscores;
5) use string in switch;
6)
Binary literal;
7) Simplify variable parameter method calls.
51cto editing recommendations:
Special topics in Java 7
Let's take a closer look at these seven new features:
Language Support for collection classes
Java will support the first type of language used to create a collection class. This means that the creation of collection classes can be like Ruby and Perl.
This was originally required:
- List list = new ArrayList();
- list.add("item");
- String item = list.get(0);
-
- Set set = new HashSet();
- set.add("item");
-
- Map map = new HashMap();
- map.put("key", 1);
- int value = map.get("key");
Now you can:
- List list = ["item"];
- String item = list[0];
-
- Set set = {"item"};
-
- Map map = {"key" : 1};
- int value = map["key"];
These sets are unchangeable.
Automatic Resource Management
Some resources in Java need to be disabled manually, such as inputstream, writes, sockets, and SQL
Classes. This new language feature allows the try statement itself to apply for more resources. These resources act on the try code block and are automatically disabled.
This:
- BufferedReader br = new BufferedReader(new FileReader(path));
- try {
- return
br.readLine();
- }
finally
{
- br.close();
- }
Changed to this:
- try (BufferedReader br = newBufferedReader(new FileReader(path)) {
- return br.readLine();
- }
You can define to close multiple resources:
- try (
- InputStream in = new FileInputStream(src);
- OutputStream out = new FileOutputStream(dest))
- {
- // code
- }
To support this behavior, all closed classes are modified to implement a closable interface.
Enhanced type inference for general-purpose instance creation (diamond)
Type inference is a special headache. The following code:
- Map> anagrams = new HashMap>();
After the data type is inferred, it becomes:
- Map> anagrams = new HashMap<>();
This <> is called the diamond (diamond) operator, which infers the type from the referenced declaration.
Number literal underline support
Long numbers are not readable. in Java 7, you can use underscores to separate long int and long, for example:
- int one_million = 1_000_000;
Use string in switch
In the past, you could only use number or enum in the switch. Now you can use 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;
- }
Binary literal
Because the C language is inherited, Java code traditionally forces programmers to use only decimal, octal, or hexadecimal notation to represent numbers (numbers ).
Because few domains are bit-oriented, such restrictions may lead to errors. You can use the 0b prefix to create a binary literal:
- int binary = 0b1001_1001;
Simplified Variable Parameter call
When a programmer tries to use an materialized Variable Parameter and calls a * varargs * (variable) method, the editor generates a "unsafe operation" Warning. JDK
7. Transfer the warning from call to method declaration (methord
In the declaration process. In this way, the API designer can use vararg because the number of warnings is greatly reduced.