Diamond syntax of Java7 new features

Source: Internet
Author: User

Diamond syntax of Java7 new features


Java 7 also introduces a change this means less typing for your when dealing with
Generics. One of the problems with generics are that the definitions and setup of
Instances can really verbose. Let's suppose that's some users, whom you
Identify by UserID (which is a integer), and each user have one or more lookup tables
specific to that user. What would.



Map<integer, map<string, string>> userslists =new Hashmap<integer, map<string, String>> ();

that's quite a mouthful, and almost half of it is duplicated characters. Wouldn ' t it be
Better if could write something like this,

Map<integer, map<string, string>> userslists = new hashmap<> ();

and has the compiler work out the type information on the right side? Thanks to the
Magic of Project Coin, you can. In Java 7, the shortened form for declarations.
is entirely legal. It ' s backwards compatible as well, so when you find yourself revisiting
Old code, you can cut the older, more verbose declaration and start using the new
type-inferred syntax to save a few pixels.
We should point out that the compiler are using a new form of type inference for
This feature. It's working out the correct type is the expression on the right side, and
isn ' t just substituting in the text that defines the full type.


The "Diamond Syntax" name
This form was called "diamond syntax" because, well, the shortened type information
Looks like a diamond. The proper name in the proposal is "improved Type inference
For Generic Instance Creation, "which are a real mouthful and have Itigic as an acronym, which sounds stupid, so diamond syn Tax it is.

Reading notes: The well-grounded Java develope

Diamond syntax of Java7 new 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.