• 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

  • 相关阅读:
    hdu 4293 2012成都赛区网络赛 dp ****
    hdu 4291 2012成都赛区网络赛 矩阵快速幂 ***
    hdu 4288 线段树 暴力 **
    hdu 4278 2012天津赛区网络赛 数学 *
    zoj 3888 线段树 ***
    虚拟化技术详解
    BRD、MRD 和 PRD 之间的区别与联系有哪些?
    salt-master配置、salt-minion配置
    Shell 脚本实现 Linux 系统监控
    Centos 7 安装 Xen
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7125652.html
Copyright © 2020-2023  润新知