java7新特性之Diamond syntax
来源:互联网 发布:编辑vi linux 编辑:程序博客网 时间:2024/05/19 10:56
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
- java7新特性之Enhanced syntax for numeric literals
- java7新特性之新语法1
- java7新特性之新语法2
- java7新特性之Improved exception handling
- Java7新特性之遍历目录树
- java7.的新特性!
- Java7新特性
- java7 新特性
- java7 新特性
- Java7新特性
- java7 新特性
- java7新特性(1)
- java7新特性
- JAVA7新特性
- Java7语法新特性
- java7 语法新特性
- Java7新特性
- hdu ACMSteps 5.3.8 Rotate
- 三色排序(荷兰国旗问题)
- Dijkstra单源最短路径
- CodeForces 148BEscape(数学)
- scrollview的相关问题
- java7新特性之Diamond syntax
- 蓝桥杯 算法训练 大小写转换
- Wu-Manber字符串多模式匹配
- poj3928 Ping pong 树状数组
- 第一章“写给-1到3岁的产品经理”读后感
- 冒泡排序思想解析及其实现(java)(2)
- IM同步服务
- ngrok-本地内网端口映射到外网(简单!有效!)
- 算法基础:递归---Fibonacci数列