码迷,mamicode.com
首页 > 编程语言 > 详细

java7新特性之Diamond syntax

时间:2017-07-06 13:23:31      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:map   ini   write   magic   rds   led   eth   infer   format   

java7新特性之Diamond syntax


Java 7 also introduces a change that means less typing for you when dealing with
generics. One of the problems with generics is that the definitions and setup of
instances can be really verbose. Let’s suppose that you have some users, whom you
identify by userid (which is an integer), and each user has one or more lookup tables
specific to that user. What would that look like in code?

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 you could write something like this,

Map<Integer, Map<String, String>> usersLists = new HashMap<>();

and have 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 like that
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 is using a new form of type inference for
this feature. It’s working out the correct type for 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 is 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 is a real mouthful and has ITIGIC as an acronym, which sounds stupid, so diamond syntax it is.


读书笔记:The Well-Grounded Java Develope

java7新特性之Diamond syntax

标签:map   ini   write   magic   rds   led   eth   infer   format   

原文地址:http://www.cnblogs.com/lxjshuju/p/7125653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!