When Java evaluates a conjunction ( && ), does it eval exp2 if exp1 is false?

前端 未结 5 628
谎友^
谎友^ 2020-12-07 04:08

I\'m wondering if it\'s guaranteed that in a Java program, the boolean expression on the right of a conjunction (exp2 above) will NOT be evaluated as long as the expression

5条回答
  •  感情败类
    2020-12-07 04:30

    No, java uses Short circuit evaluation. If expr1 is false, expr2 will not be evaluated, thus your && usage is perfectly safe.

    Also, if you have if (exp1 || exp2) { .. } - exp2 will not be evaluated if exp1 is true.

提交回复
热议问题