1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
Passing an argument of type `Class` to `Class#instanceOf(Class)` is usually a
mistake.
Calling `clazz.instanceOf(obj)` for some `clazz` with type `Class<T>` is
equivalent to `obj instanceof T`. The `instanceOf` method exists for cases where
the type `T` is not known statically.
When a class literal is passed as the argument of `instanceOf`, the result will
only be true if the class literal on left hand side is equal to `Class.class`.
For example, the following code returns true if and only if the type `A` is
equal to `Class` (i.e. lhs is equal to `Class.class`).
```java
<A, B> boolean f(Class<A> lhs, Class<B> rhs) {
return lhs.instanceOf(rhs); // equivalent to 'lhs == Class.class'
}
```
To test if the type represented by a class literal is a subtype of the type
reprsented by some other class literal, `isAssignableFrom` should be used
instead:
```java
<A, B> boolean f(Class<A> lhs, Class<B> rhs) {
return lhs.isAssignableFrom(rhs); // equivalent to 'B instanceof A'
}
```
|