[Solved] What does this expression evaluate to? [closed]


In Java, the test

if (i==f && i.equals(f))

is nonsensical. Since i is an Integer and f is a Float, they will never be == (and, since they are incommensurate types, cannot legally be compared with ==). For reference types, the == operator evaluates to true only if the variables reference the same object, which they cannot do with objects of different types. Consequently, since i==f evaluates to false, the second part will never be evaluated because the && operator is a “short circuit” boolean operator.

I suppose if i and f were of some other type, this might be a way to check that the class’s equals() method was reflexive (as required by the spec, but there are always programming bugs). However, it would make more sense to have:

if (i != null && i.equals(f))

to avoid a potential NullPointerException.

2

solved What does this expression evaluate to? [closed]