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,则右侧不再计算,也就是所谓的短路或效果。

目的

网上有些文档有误人子弟嫌疑,特溯本清源,以正视听。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值