zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Win32中使用目录浏览器
    xcode 决断设备类型
    一个简单观察者模式的实现 cocos2dx
    在 vector , map , list 间取得平衡
    ChainOfResponsibility 与 Command
    贪吃蛇游戏制作
    Unicode形成的“?”问题
    MFC中手工创建控件
    CString操作指南【转】
    一个简单例子的设计模式分析
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7125652.html
Copyright © 2011-2022 走看看