Re: Parentheses issue
"jupiter" <jupiter49byebyeSpam@msn.com> writes:
if(obj instanceof AccountBalance &&
(AccountBalance)obj.getBalance() == this.balance) {
return true;
}
... the compiler complains of
"incompatible operand types, AccountBalance and double"
Patrick May wrote:
The error message is certainly not descriptive.
Seems descriptive enough to me. It says that the operands, in this case of ==,
are of incompatible types. The right side is clearly the "double" of the
comparison, so the left side clearly must be the "AccountBalance". This it
clearly is, because the cast makes it so. Therefore, clearly, the operands are
incompatible, just as the error message clearly describes. The message, in
fact, is entirely descriptive. Certainly.
As for the error itself, one should be at least a little bit aware of operator
precedence (*not* "order of operations", which can change), and other Java
language fundamentals, if one is to be a Java programmer. One should be
completely aware that there exist rules of operator precedence, and what the
term means.
That said, it is good practice to use a few extra parentheses to clarify the
order as expressions get more complex, since some of the precedence rules may
be non-obvious even to the experienced.
A closely related topic is operator associativity. What is the interpretation of
a == b? c == d? x : y : e == f? z : null;
just to invent one mildly pathological case. Parentheses would help
readability here, too.
- Lew