Java operators precedence

前端 未结 2 1340
渐次进展
渐次进展 2020-12-07 03:04

I have got confused due to what is true regarding the operator precedence in Java. I read in tutorials a long time ago that AND has a higher priority than OR, which is confi

相关标签:
2条回答
  • 2020-12-07 03:49

    Because of the short-circuit &&, the expression is evaluated as though there were parentheses around (y < 2) | doStuff() ...

    This statement is incorrect, indeed meaningless. The fact that && is a short-circuit operator has nothing to do with the evaluation of (y < 2) | doStuff(), and indeed that could only compile if doStuff() returned a Boolean. What makes the difference (the implicit parentheses) is the precedence of && relative to |, which is defined in JLS ##15.22-23 as && being lower.

    0 讨论(0)
  • 2020-12-07 04:07

    That's because it is using the | operator instead of ||, which has a higher priority. Here's the table.

    Use the || operator instead and it'll do what you think.

    0 讨论(0)
提交回复
热议问题