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

  • 相关阅读:
    Java程序性能优化——让你的java程序更快、更稳定
    synchronized和ReentrantLock
    Java集合——ConcurrentHashMap
    SpringMVC流程
    计算机网络http,https,tcp,udp,get,post
    JVM类加载机制
    关于strcpy和memcpy
    C语言指针
    malloc函数详解
    进程和线程
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7125652.html
Copyright © 2011-2022 走看看