Re: Parentheses issue




"Patrick May" <pjm@xxxxxxx> wrote in message
news:m2vejsztbf.fsf@xxxxxxxxxx
"jupiter" <jupiter49byebyeSpam@xxxxxxx> writes:
Can somebody explain in English

Will you accept geeklish?


Bring it on!


why the cast of obj to
AccountBalance works here?

if(obj instanceof AccountBalance &&
((AccountBalance)obj).getBalance() == this.balance) {
return true;
}

But doesn't work here?

if(obj instanceof AccountBalance &&
(AccountBalance)obj.getBalance() == this.balance) {
return true;
}

The object member operator ('.') has higher precedence than
the
cast operator ('(...)'). The second form tries to call
getBalance()
before casting obj to the AccountBalance type.

Nice explanation. Is that actually geekish? I've heard much
worse, hehe.


In the second snippet the compiler complains of "incompatible
operand types, AccountBalance and double"

The error message is certainly not descriptive.

Sorry for the lack of documentation; it was comparing doubles (or
was supposed to be until the cast failed.)

Thanks, Patrick.



.



Relevant Pages

  • Re: Parentheses issue
    ... AccountBalance works here? ... if(obj instanceof AccountBalance && ... The object member operator has higher precedence than the ... before casting obj to the AccountBalance type. ...
    (comp.lang.java.programmer)
  • Parentheses issue
    ... Can somebody explain in English why the cast of obj to ... AccountBalance works here? ... if(obj instanceof AccountBalance && ...
    (comp.lang.java.programmer)