Re: Incomparable types
"Patricia Shanahan" <pats@acm.org> wrote in message
news:7p9Ag.2315$xp2.360@newsread1.news.pas.earthlink.net...
I can see the rule that is being applied, and it is stated in the JLS,
but the rule itself makes no sense to me.
The language allows many comparisons that can never have a true result.
For example, 2==3 is a valid comparison, with the result false.
Likewise:
byte b;
...
if (b > 500)
and
Object o;
...
if (o == new Object())
Both are legal yet can never be true.
Trivially true type comparisons, such as ("A" instanceof String) are
accepted.
It is only in the context of type comparisons, and only if the result is
false, that the compiler rejects the comparison at compile time.
It seems as if this is an attempt to make Java seem more strongly typed than
it actually is, so that e.g.
int i;
Integer j;
String s;
...
if (s == j)
is deemed not false but undefined, the way that
if (s == i)
is, in fact, not false but undefined, since equality of references and
scalars is not defined.
This is nonsense, of course. Determining whether two references are equal
is perfectly well defined, as shown by the legality of:
if ((Object)s == (Object)i)
where the casts do not change the value of the references, but simply remove
the compiler's unwillingness to generate the proper code.