--java7 new features of the path of vegetable cultivation

Source: Internet
Author: User

1, TWR automatically shut down resources

Automatic closing of resources you must be aware of the writing specification, and all resources that need to be closed must be declared with a variable and written in the block of parentheses in the try, not curly braces. The following code:

Try ( InputStream is = new FileInputStream (new File )) {

Code that might have occurred an exception

}catch (Exception e) {

Exception information capture

}finally{

Automatically shuts down resources without manual processing of IS

}


2. Switch new string support

Before JAVA7 switch just supports the byte short char int enumeration as well as the byte short char Integer, which is now a new support string, in fact JavaScript always supports strings.


3. Grammar Sugar

(A grammar that is added to a computer language that has no effect on the functionality of the language, but is more convenient for programmers to use.) In general, the use of syntactic sugars can increase the readability of the program, thus reducing the chance of program code errors.

Improvements to the numeric constants, new binary notation, numbers can use underscores, compile-time automatically remove the underscore, such as the following code:

int value = 123_456_789;//increases the readability of the Code

int value = 0b1100111;//binary represents a number


4. Multi-Catch declare multiple exceptions to be caught in a catch block

try{

Code that might have occurred an exception

}catch (IOException | FileNotFoundException e){

Exception information capture

}catch (Exception e) {

Exception information capture

}finally{

Automatically shuts down resources without manual processing of IS

}


5. Final exception

In order to avoid the restriction of the type when the exception was thrown, JDK7 introduced the final exception, as in the following code:

try{

Code that might have occurred an exception

}catch (IOException | FileNotFoundException e){

Exception information capture

}catch (final Exception e) {

Exception information capture

Throw e; The top-level interface is thrown here, but if the final tag is used, it is the true exception type.

}finally{

Automatically shuts down resources without manual processing of IS

}


6, diamond syntax, that is, in generics simplifies the initialization of generics syntax

map<string,string> map = new hashmap<> ();  


7. Simplified method invocation of the variable parameter

The use of variable parameters, as well as generic automatic alarms, are slightly altered.

@SafeVarargs

public static <T> t Usevarargs (t ... args) {

Return args.length > 0? ARGS[0]: null;

}

Varargswarning.usevarargs (New arraylist<string> ());  



Note: Not only do you need to know the new syntax, but to get a deeper understanding of why and how to implement these new features!


--java7 new features of the path of vegetable cultivation

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.