[Read notes] New Features of Java 7

Source: Internet
Author: User

Time: 1.5 hours
Original article: A look at Java 7's new features, by madhusudhan Konda
Http://radar.oreilly.com/2011/09/java7-features.html

 

1. Introduced "diamond operator" (statement: <>)

Description in one sentence: Simplifies the syntax for creating parameterized class objects.
Example:
(Before 7) Map <string, list <trade> trades = new treemap <string, list <trade> ();
(Since 7) Map <string, list <trade> trades = new treemap <> ();
Notes:
Trades = new treemap () is also valid, but the compiler will prompt "type-safety warnings"
Comment:
To avoid this tedious syntax before Java 7, we should use static factory method to implement it. This is described in article 1 of objective Java 2nd, joshua mentioned in his book "Someday the language may perform this sort of type inference on constructor invocations as well as method invocations. ", of course, this feature is now supported in this version.

2. Switch supports the string type

Description in one sentence: None.
Example: None:
Notes: Call the string. Equals () method to compare the case values.
Comment:
1. Switch supports the following types: primitive types, Enum, string
2. using string is more error-prone than Enum and reduces the risk of errors: (1) Defining string as a constant, and using only constants to reference strings in case (2) if business logic permits, the default statement does not hesitate to throw an exception.

3. Automatic Resource Management

 Description in one sentence:Try-finallyTo ensure that resources are released.
Example:
(Before 7)
Try{
Fos=NewFileoutputstream ("Movies.txt"); DoS=NewDataoutputstream (Fos); DoS. Writeutf ("Java 7 Block Buster"); }Catch(Ioexception e ){ E. printstacktrace (); }Finally{ Try{ Fos. Close (); DoS. Close (); }Catch(Ioexception e ){ // Log the exception } } (Since 7)Try(Fileoutputstream Fos =NewFileoutputstream ("Movies.txt");
Dataoutputstream dos =NewDataoutputstream (FOS )){ Dos. writeutf ("Java 7 Block Buster");
}Catch(Ioexception e ){ // Log the exception

}

Notes:

1. The resources in this structure must implement the java. Lang. autocloseable interface (this interface has only one close method)

2. In the impression, C # has long had similar features.

3. ObserveCodeIn the (before 7) version, there are two catch statements with ioexception, which correspond to two different scenarios. Therefore, there are different processing logics in the catch statement block;

But (since 7) has only one catch. How can we distinguish different situations?Check the compiled bytecode to confirm..

4. Underlined numbers

Description in one sentence: Reading long numbers is more convenient.

Example: Int million = 0000000_000

Comment: Whether this feature is widely accepted by developers.

5. Improvement of the Exception Handling Mechanism

One sentence description:
Multiple catch blocks can be merged.

Example:

Try{

Methodthatthrowsthreeexceptions ();

}Catch(Exceptionone | exceptiontwo | exceptionthree e ){

// Log and deal with all exceptions

}

Comment:

The exception with the same processing logic can be written together to reduce code redundancy.

6. New file system API (NIO 2.0)

Description in one sentence: Makes it easier to develop code that supports multiple file systems.

New content includes: 1. java. NiO. file. The new classes include path, paths, filesystem, filesystems, and so on; 2. File Change notifacation

7. Fork and join

Description in one sentence: Assign the task to multiple cores for execution, and then summarize

Description: Several related classes

Forkjoinpool: used to execute forkjointask and implement executor and executorservice.

Forkjointask: The problem/task to be solved.

Recursiveaction: a subclass of forkjointask. No value is returned.

Recursivetask: subclass of forkjointask, return value.

Executor: Implemented by forkjoinpool.

8. Supporting dynamism (invokedynamic)

Description in one sentence: Provides support for dynamic languages.

Description: Check whether the data is compiled or run based on the type. It can be divided into "static typed language" (such as Java) and "dynamically typed language" (such as Ruby, Python, clojure ). Java 7 has a new package, java. Lang. invoke, to support dynamic languages. To learn about this feature, you need special materials.

Related Article

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.