I am getting the following code snippet from Android’s Pair.java
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Pair)) return false;
final Pair<F, S> other;
try {
other = (Pair<F, S>) o;
} catch (ClassCastException e) {
return false;
}
return first.equals(other.first) && second.equals(other.second);
}
I was wondering, how is it possible to have ClassCastException, after instanceof returns true.
It isn’t possible. The code makes no sense. Whoever wrote it probably didn’t understand that
FandSare erased at runtime so aClassCastExceptioncould never happen.