Seven new functions completed by Java 7:
1. Language Support for collection classes;
2. Automatic Resource Management;
3. Improved general instance creation type inference;
4. Support for literal underscores;
5. Use string in switch;
6. Binary literal;
7. Simplify variable parameter method calls.
Let's take a closer look at these seven new features:
1. 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 <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 ");
Now you can:
List <string> List = ["item"];
String item = list [0];
Set <string> set = {"item "};
Map <string, integer> map = {"key": 1 };
Int value = map ["key"];
These sets are unchangeable.
2. 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 = new bufferedreader (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.
3. Enhanced type inference for general-purpose instance creation (diamond)
Type inference is a special headache. The following code:
Map <string, list <string> anagrams = new hashmap <string, list <string> ();
After the data type is inferred, it becomes:
Map <string, list <string> anagrams = new hashmap <> ();
This <> is called the diamond (diamond) operator, which infers the type from the referenced declaration.
4. Support for literal underscores
Long numbers are not readable. in Java 7, you can use underscores to separate long int and long, for example:
Int one_million = 0000000_000;
Remove the underline before calculation, for example, 1_1*10 = 110,120-1_0 = 110
5. 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;
}
6. 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;
Now, you can use the binary literal representation and use very short code to convert binary characters to data types, such as in bytes or short.
Byte abyte = (byte) 0b001;
Short ashort = (short) 0b010;
7. Simplified Variable Parameter calls
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 transfers the warning from call to the methord declaration process. In this way, the API designer can use vararg because the number of warnings is greatly reduced.
Transfer from Renren programmers