What is the difference between <? extends Base> and ?

前端 未结 4 1543
温柔的废话
温柔的废话 2021-02-01 03:24

In this example:

import java.util.*;

public class Example {
    static void doesntCompile(Map> map) {}
    static &l         


        
4条回答
  •  佛祖请我去吃肉
    2021-02-01 04:06

    In the call:

    compiles(new HashMap>());
    

    T is matched to Integer, so the type of the argument is a Map>. It's not the case for the method doesntCompile: the type of the argument stays Map> whatever the actual argument in the call; and that is not assignable from HashMap>.

    UPDATE

    In the doesntCompile method, nothing prevents you to do something like this:

    static void doesntCompile(Map> map) {
        map.put(1, new ArrayList());
    }
    

    So obviously, it cannot accept a HashMap> as the argument.

提交回复
热议问题