A few useful Java 7 features

Source: Internet
Author: User
Tags stream api java se

Not to be admitted, the release of Java SE 9 is only 2 months away. But I am very low, until recently began to use Java SE 7 in practice some of the new features brought to the programmer. Not to mention the lambda expression of Java SE 8, the Stream API, and other advanced features. So, in my future several blogs, will introduce some new features starting from Java SE 7. Writing these things is also to consolidate their own learning, because the author himself is a learner, not what Daniel. All right, crap, that's it. The author will introduce several very practical Java 7 features, the text if the useful words or professional terminology is not allowed phenomenon, hope forgive me!

A few practical Java 7 features:

1. Exception Handling Improvements:

In Java SE7, a catch can catch multiple exceptions, which can reduce duplication of code. Between each exception with | Separated. For example, the following form:

try {

The specific processing code
} catch (Instantiationexception | illegalaccessexception | ClassNotFoundException e) {

How to handle catching exceptions

}

Before Java SE 7, however, you must take the following form for the capture of the exception:

try {

The specific processing code
}catch (instantiationexception) {

How to handle catching exceptions

}

catch (illegalaccessexception) {

How to handle catching exceptions

}

catch (ClassNotFoundException) {

How to handle catching exceptions

}

Note that if a catch handles multiple exceptions, the catch parameter defaults to final, and you cannot modify its value in the catch block.
In addition, handling multiple exceptions with a catch is smaller and more efficient than the bytecode generated by each processing of one exception with multiple catch.

2. Try-with-resources statement:

Note: The try-with-resources author's understanding is to write code that opens a resource in a try statement block. Java 7 needs to close the socket, file, database connection and other resources in Finally, Java 7 in the Try statement to request resources, you can achieve the automatic release of resources (resource classes must implement the Java.lang.AutoCloseable interface, general files, The interface is implemented by the database connection, and the Close method is automatically called). For example, the following code:

Try (Scanner in = new Scanner (Paths.get ("/user/share/dict/words"));

PrintWriter out = new PrintWriter ("/tmp/out.txt")

)   {

while (In.hasnext ())

Out.println (In.next (). toLowerCase ());

}

In this way, no matter how the code block exits, they will be closed as long as the in and out objects have been created before. Eliminates the cumbersome code to actively shut down resources.

3.nio2, this feature primarily refers to the use of path paths. Using it requires only one line of code to read the entire contents of a file: for example:

byte[] bytes = files.readallbytes (path);

If you want to read the contents of a file as a string, you can call it after ReadAllBytes:

String content = new String (bytes, standardcharses.utf_8);

4. In the method of converting a string to a number, the string can be "+", "-" and so on:

For example: Double x = double.parsedouble ("+1.0"), is the correct code and does not throw an exception.

5. Check for null:

The objects class provides a Requirenonnull method to check whether a parameter is null, such as the following example:

public void process (String content) {

This.content = objects.requirenonnull (content);

}

In the above code, if the content is null, then throws a null pointer exception, this seemingly not much effect of the improvement is beneficial to the program hints, in fact, can also give the resulting exception to provide a message string, like this form:

public void process (String content) {

This.content = objects.requirenonnull (content, "the content can is not null");

}

A few useful Java 7 features

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.