java7新特性之Diamond syntax
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新特性之Try-with-resources (TWR)
java7新特性之Try-with-resources (TWR) This change is easy to explain, but it has proved to have hidden s ...
- java7 新特性 总结版
Java7语法新特性: 前言,这是大部分的特性,但还有一些没有写进去,比如多核 并行计算的支持加强 fork join 框架:这方面并没有真正写过和了解.也就不写进来了. 1. switch中增加对S ...
- java7新特性 java8新特性
Java 7 的7个新特性 Java7语法新特性 JAVA8 十大新特性详解 http://www.jb51.net/article/48304.htm
- Java7 新特性 数值文本表示法
今天和大家分享下 java7中新特性-数值文本表示法 首先,在原来jdk1.6中 如果需要将一个二进制的数值转换成十进制的话,一般情况下都会以下面的代码方式去实现. public static voi ...
- Java7新特性
① 新增了switch对字符串的支持,也就是说可以在switch之后直接使用字符串来进行判断,语法基本与Java7之前支持的语法一样. ② 对数值字面量的增强支持,首先是可以在源代码中直接使用二进制数 ...
- Java7 新特性: try-with-resources
Try-with-resources是java7中一个新的异常处理机制,它能够很容易地关闭在try-catch语句块中使用的资源. 利用Try-Catch-Finally管理资源(旧的代码风格)在ja ...
- Java7 新特性 —— java.nio.file 文件操作
本文部分摘自 On Java 8 自 Java7 开始,Java 终于简化了文件读写的基本操作,新增了 java.nio.file 库,通过与 Java8 新增的 stream 结合可以使得文件操作变 ...
- Java7 新特性 switch 可以使用String
今天和大家分享下 在java7中可以使用String 作为switch 中的参数. 原来在java7之前,switch只能去接收一个 byte.char.short.int 类型 现在在java7中 ...
- Java之Java7新特性之try资源句式
一.原来写法: static String readFirstLineFromFile(String path) throws IOException { BufferedReader br = nu ...
随机推荐
- Dynamic Web Module版本对应tomcat版本
MyEclipse2017+JDK 1.8环境下 Dynamic Web Module版本3.1要对应tomcat7.0以上版本,不然部署项目时会出现错误(会出现无法部署项目的情况). Dynamic ...
- 下载kaggle数据集,验证手机号
https://blog.csdn.net/Tomxiaodai/article/details/80167765 kaggle上下载一下数据集必须手机验证,结果验证时一直提示错误输入的格式错误,试了 ...
- TX-LCN事务控制原理
TX-LCN由两大模块组成, TxClient.TxManager,TxClient作为模块的依赖框架,提供TX-LCN的标准支持,TxManager作为分布式事务的控制方.事务发起方或者参与方都由T ...
- Json Web Token(JWT)详解
什么是Json Web Token Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的 ...
- 3D超炫酷旋转
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 零基础入门学习Python(13)--元组:戴上了枷锁的列表
前言 这节课我们讨论主题是元祖:我们有个小标题戴上了枷锁的列表 我们都知道早在300多年前,孟德斯鸠在变法的时候说过,一切拥有权力的人都容易被滥用权力,这是万古不变的一条经验.但是呢,凡是拥有大权利的 ...
- 在rubymine中集成heroku插件
先安装heroku,参见http://www.cnblogs.com/jecyhw/p/4906990.html Heroku安装之后,就自动安装上git,目录为C:\Program Files (x ...
- 集训第四周(高效算法设计)E题 (区间覆盖问题)
UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...
- amoeba连接mysql--ERROR 2006 (HY000): MySQL server has gone away
amoeba下载地址:http://sourceforge.net/projects/amoeba/files amoeba version:amoeba-mysql-binary-2.1.0-RC5 ...
- 【区间DP+好题】String painter
https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/G [题意] 给定两个长度相同的字符串A,B.每次操作都能把A中的任意一个子段变成 ...