The difference between “instanceof List” and 'o instanceof List<?>"

后端 未结 2 1702
太阳男子
太阳男子 2021-01-21 03:20

I don\'t see any difference in the following:

    Object o = new LinkedList();

    System.out.println(o instanceof List); 
    System.out.println(o          


        
相关标签:
2条回答
  • 2021-01-21 03:50

    According to this blog the answer is 'they are exactly the same':

    as javac forbids instanceof expressions whose target type is a generic type; for casts, the compiler is slightly more permissive since casts to generic type are allowed but a warning is issued (see above). Anyway, the raw type should be replaced by an unbounded wildcard, as they have similar properties w.r.t. subtyping.

    Object o = new ArrayList<String>();
    List<?> list_string = (List)o; //same as (List<?>)o
    boolean b = o instanceof List; //same as o instanceof List<?>
    
    0 讨论(0)
  • 2021-01-21 04:13

    No difference. The wildcard is erased at compile time.

    0 讨论(0)
提交回复
热议问题