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

  • 相关阅读:
    如何清除el-dialog中的表单验证
    购物车小球飞入动画
    node的应用场景
    如何在Ubuntu Server 18.04 LTS中配置静态IP地址
    在Ubuntu上启用和禁用NetworkManager
    gcc常用命令
    cmake入门
    模拟实现pwd命令
    模拟实现ls命令
    qmake生成的Makefile在make install时遇到的一次错误
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7125653.html
Copyright © 2011-2022 走看看