What is the difference between & and && in Java?

前端 未结 13 2696
感情败类
感情败类 2020-11-22 10:33

I always thought that && operator in Java is used for verifying whether both its boolean operands are true, and the & oper

13条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 10:56

    && and || are called short circuit operators. When they are used, for || - if the first operand evaluates to true, then the rest of the operands are not evaluated. For && - if the first operand evaluates to false, the rest of them don't get evaluated at all.

    so if (a || (++x > 0)) in this example the variable x won't get incremented if a was true.

提交回复
热议问题