The following code generates a warning Unchecked cast: 'T' to 'U' in IntelliJ IDEA:
interface A {}
class B<T extends A, U extends A> {
void f() {
final T t = null;
final U u = (U) t;
}
}
This doesn't make sense to me, since T and U are defined as the same type. What is the problem?
TandUare not defined as the same type. They are both defined asextends A, which meansTandUcan be unrelated classes that implement theAinterface. Hence the cast is not safe.The only safe cast you can do is to cast references of type
TorUto typeA. Of course you don't need such a cast. You can simply assign them to a variable of typeA.