Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to have ClassCastException after instanceof returns true

Tags:

java

android

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.

like image 744
Cheok Yan Cheng Avatar asked Sep 25 '12 14:09

Cheok Yan Cheng


1 Answers

It isn't possible. The code makes no sense. Whoever wrote it probably didn't understand that F and S are erased at runtime so a ClassCastException could never happen.

like image 177
artbristol Avatar answered Sep 27 '22 20:09

artbristol