How is the generic type getting inferred here?

后端 未结 3 422
一整个雨季
一整个雨季 2021-01-18 00:23
public static void main(String[] args) {
    Map>> map = getHashMap();
}

static  Map getHashM         


        
3条回答
  •  滥情空心
    2021-01-18 00:43

    The getHashMap function does not have to infer the types. It's at the call site that javac is required by the Java Language Spec to infer that the types agree (15.12.2.7 Inferring Type Arguments Based on Actual Arguments).

    I believe the current plan is (still) for JDK7 to support the diamond operator, so this sort of thing will work with new too, although with a bit of apparently pointless syntax.

    Map>> map = new HashMap<>();
                                                          ^^diamond
    

提交回复
热议问题