I don't see any difference in the following:
我看不出有什么区别:
Object o = new LinkedList<Long>();
System.out.println(o instanceof List);
System.out.println(o instanceof List<?>);
Is there any practical use of instanceof List<?>
when instanceof List
can't be used instead and vise versa?
是否有实用的instanceof列表
2 个解决方案
#1
8
No difference. The wildcard is erased at compile time.
没有区别。在编译时,通配符将被删除。
#2
3
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.
由于javac禁止instanceof表达式,其目标类型是泛型类型;对于类型转换,编译器会稍微宽容一些,因为对泛型类型的转换是允许的,但是会发出警告(见上面)。无论如何,原始类型应该被一个未绑定的通配符替换,因为它们有类似的属性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<?>
#1
8
No difference. The wildcard is erased at compile time.
没有区别。在编译时,通配符将被删除。
#2
3
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.
由于javac禁止instanceof表达式,其目标类型是泛型类型;对于类型转换,编译器会稍微宽容一些,因为对泛型类型的转换是允许的,但是会发出警告(见上面)。无论如何,原始类型应该被一个未绑定的通配符替换,因为它们有类似的属性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<?>