New Features of Java 7

Source: Internet
Author: User

[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:

 
 
  1. List list = new ArrayList();  

  2. list.add("item");  

  3. String item = list.get(0);  

  4.  

  5. Set set = new HashSet();  

  6. set.add("item");  

  7.  

  8. Map map = new HashMap();  

  9. map.put("key", 1);  

  10. int value = map.get("key");  

Now you can:

 
 
  1. List list = ["item"];  
  2. String item = list[0];  

  3.  

  4. Set set = {"item"};  
  5.  

  6. Map map = {"key" : 1};  

  7. 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:

 
 
  1. BufferedReader br = new BufferedReader(new FileReader(path));  

  2. try {  

  3. return
     br.readLine();  


  4. finally
     {  

  5.    br.close();  



Changed to this:

 
 
  1. try (BufferedReader br = newBufferedReader(new FileReader(path)) {  

  2.    return br.readLine();  



You can define to close multiple resources:

 
 
  1. try (  

  2.    InputStream in = new FileInputStream(src);  

  3.    OutputStream out = new FileOutputStream(dest))  

  4. {  

  5.  // 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:

 
 
  1. Map> anagrams = new HashMap>(); 

After the data type is inferred, it becomes:

 
 
  1. 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:

 
 
  1. 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:

 
 
  1. String s = ...  

  2. switch(s) {  

  3.  case"quux":  

  4.     processQuux(s);  

  5.     // fall-through
  6.  

  7.   case"foo":  

  8.   case"bar":  

  9.     processFooOrBar(s);  

  10.    break;  

  11.  

  12. case"baz":  

  13.      processBaz(s);  

  14.     // fall-through

     

  15.  

  16.  default:  

  17.   processDefault(s);  

  18.    break;  

  19. }  


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:

 
 
  1. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.