Parentheses issue



Hi guys,
Can somebody explain in English 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;
}


In the second snippet the compiler complains of "incompatible
operand types, AccountBalance and double" so it is apparently not
even noticing the obj.getBalance() method. Or maybe it needs to
only weigh the leftmost object versus the right hand operand, and
I've separated or broken up the lefthand operand? I expected that
the cast would be applied first, making obj a type of
AccountBalance, and then getBalance() would be parsed.

Not true but I don't know why.





.



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)
  • Re: Parentheses issue
    ... AccountBalance works here? ... if(obj instanceof AccountBalance && ... cast operator '). ... before casting obj to the AccountBalance type. ...
    (comp.lang.java.programmer)