java 基础 逻辑与 与 短路与 纠错
javase7 官方文档引用 参考在这.
15.22.2. Boolean Logical Operators &, ^, and |
When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. In all cases, the operands are subject to unboxing conversion (§5.1.8) as necessary.
For |, the result value is false if both operand values are false; otherwise, the result is true.
For &, the result value is true if both operand values are true; otherwise, the result is false.
For ^, the result value is true if the operand values are different; otherwise, the result is false.
For |, the result value is false if both operand values are false; otherwise, the result is true.
15.23. Conditional-And Operator &&
The conditional-and operator && is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.
15.24. Conditional-Or Operator ||
The conditional-or operator || operator is like | (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is false.
官方文档解释
&, | 是位运算符,对于两侧的boolean来说,是逻辑运算符,即通常所说的与或非运算符的成员。
- & 两侧均为True,结果才为True;其他情况皆为False。左侧为False时,右侧也会运算结果。
- | 两个有一个为True,结果即为True;皆为False,结果才为False。左侧为True,右侧也会计算。
&&, ||是条件逻辑运算符,也就是所谓的短路与运算符。
- && 与两侧为boolean的&类似,区别在于 如果左侧为False,则右侧不再计算,也就是所谓的短路与效果。
- || 与两侧为boolean的|类似,区别在于 如果左侧为True,则右侧不再计算,也就是所谓的短路或效果。
目的
网上有些文档有误人子弟嫌疑,特溯本清源,以正视听。