6

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.

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

3

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.

artbristol
  • 32,010
  • 5
  • 70
  • 103