Java doesn't care about the generic type when you use instanceof
. To quote the Javadocs:
Because the Java compiler erases all type parameters in generic code, you cannot verify which parameterized type for a generic type is being used at runtime
So, you can test against Action<?>
or Action
, but you will get a compiler error if you try to test against Action<Integer>
. The error message is very informative:
Cannot perform instanceof check against parameterized type Action<Integer>. Use the form Action<?> instead since further generic type information will be erased at runtime