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 ...
随机推荐
- Bitmap.createBitmap函数有6个重载方法
位图剪切参考重载方法4和6,重载方法6比较简单 public static Bitmap createBitmap (Bitmap src)从原位图src复制出一个新的位图,和原始位图相同 publi ...
- 管理Fragments
FragmentManager 为了管理Activity中的fragments,需要使用FragmentManager. 为了得到它,需要调用Activity中的getFragmentManager( ...
- js中事件冒泡和事件捕获
什么时候存在这种问题? 当一个行为触发了多个对象的事件时. <body> <div class="fa"> <div class=&q ...
- oracle dmp文件的导入导出
一.命令行方式 exp 用户名/密码@库名 file=文件位置.dmp owner=用户名 imp 用户名/密码@库名 file=文件位置.dmp 注意 : 导入过程若有的表已经存在可能会报错,可以全 ...
- ubuntu 虚拟机系统调优
Ubuntu虚拟机镜像最佳实践 分区/boot >1G/root >10G/var >5G配swap空间,内存的2倍 vi /etc/secur ...
- ajax中文乱码解决(java)
方法1: 页面端发出的数据做一次encodeURI,服务器端使用new String(old.getBytes("iso8859-1"), "utf-8") 方 ...
- pip 打包项目配置库
打包项目中配置库(filename为文件名,可修改) pip freeze > filename.txt 安装配置文件中所有的库包 pip install -r filename.txt 如提示 ...
- 合办大学 -- internal campus in China
* 合办大学 -- internal campus in China- international campus zhejiang University- 南方科技大学 - 西交利物浦大学(Xi’an ...
- 06-看图理解数据结构与算法系列(AVL树)
AVL树 AVL树,也称平衡二叉搜索树,AVL是其发明者姓名简写.AVL树属于树的一种,而且它也是一棵二叉搜索树,不同的是他通过一定机制能保证二叉搜索树的平衡,平衡的二叉搜索树的查询效率更高. AVL ...
- Linux下汇编语言学习笔记50 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...