Zhangyi Hu
Zhangyi Hu

Reputation: 39

Java instanceof generic type

I am wondering why the first line compiles but the second not

new Integer(1) instanceof Comparable<?>;
new Integer(1) instanceof Comparable<Integer>;

The error msg is:

illegal generic type for instanceof

Thanks

Upvotes: 0

Views: 102

Answers (1)

Andy Turner
Andy Turner

Reputation: 140328

From JLS Sec 15.20.2:

It is a compile-time error if the ReferenceType mentioned after the instanceof operator does not denote a reference type that is reifiable (§4.7).

From Sec 4.7

A type is reifiable if and only if one of the following holds:

...

  • It is a parameterized type in which all type arguments are unbounded wildcards (§4.5.1).

...

Upvotes: 1

Related Questions