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