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

前端 未结 5 625
谎友^
谎友^ 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:54

    From the Java Language Specification, 15.23 Conditional-And Operator &&:

    The && operator is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.

    So the language spec guarantees that the right-hand side of your expression will not be evaluated if the left hand side is false.

提交回复
热议问题