int a = 6; // 110
int b = 4; // 100
// Bitwise AND
int c = a & b;
// 110
// & 100
// -----
// 100
// Bitwise OR
int d = a | b;
// 110
// | 100
// -----
// 110
System.out.println(c); // 4
System.out.println(d); // 6
Thanks to Carlos for pointing out the appropriate section in the Java Language Spec (15.22.1, 15.22.2) regarding the different behaviors of the operator based on its inputs.
boolean a, b;
Operation Meaning Note
--------- ------- ----
a && b logical AND short-circuiting
a || b logical OR short-circuiting
a & b boolean logical AND not short-circuiting
a | b boolean logical OR not short-circuiting
a ^ b boolean logical exclusive OR
!a logical NOT
short-circuiting (x != 0) && (1/x > 1) SAFE
not short-circuiting (x != 0) & (1/x > 1) NOT SAFE
短路求值 、最小求值或麦卡锡求值(在约翰 · 麦卡锡之后)是一些布尔运算符在某些编程语言中的语义,在这些语言中,只有当第一个参数不足以确定表达式的值时,才执行或求值第二个参数: 当 AND 函数的第一个参数求值为 false 时,整体值必须为 false;。
Not Safe 意味着操作符总是检查子句中的每个条件,因此在上面的示例中,当 x 实际上是0值时,可以计算1/x,从而引发异常。
If the first operand returns a value of true then the second operand is evaluated. If the second operand returns a value of true then && operator is then applied to the first and second operands.
This fails for null, as evaluating the second expression produces a NullPointerException. Logical operator && is lazy, if left operand is false, the result is false no matter what right operand is.
例如第三点——假设我们有一个使用 DB 的应用程序,它没有任何触发器或级联。在删除 Building 对象之前,必须将 Department 对象的 build 更改为另一个。我们还假设操作状态作为布尔值返回(true = Success)。然后:
if (departmentDao.update(department, newBuilding) & buildingDao.remove(building))
In Java, the single operators &, |, ^, ! depend on the operands. If both operands are ints, then a bitwise operation is performed. If both are booleans, a "logical" operation is performed.
如果两个操作数不匹配,则引发编译时错误。
双操作符 & & ,| | 的行为与单操作符类似,但两个操作数都必须是条件表达式,例如:
如((a < 0) & & (b < 0)){ ... }或类似情况,
if (( a < 0 ) || ( b < 0 )) { ... }
逻辑和:
Logical AND (aka Conditional AND) uses the & & operator. It's short-circuited meaning: if the left operand is false, then the right operand will not be evaluated. Example:
int x = 0;
if (false && (1 == ++x) {
System.out.println("Inside of if");
}
System.out.println(x); // "0"
在上面的例子中,打印到 x 控制台的值是0,因为 if 语句中的第一个操作数是 false,因此 java 不需要计算(1 = = + + x) ,因此 x 不会被计算。
按位:
按位 AND 使用 &运算符。它用来预先形成一个位操作的价值。通过观察对二进制数的运算,可以更容易地看出发生了什么,例如:
int a = 5; // 5 in binary is 0101
int b = 12; // 12 in binary is 1100
int c = a & b; // bitwise & preformed on a and b is 0100 which is 4
布尔值和:
现在,布尔 AND 操作符的行为与位 AND 和逻辑 AND 相似且不同。我喜欢把它看作是在两个布尔值(或位)之间预先形成一个按位 AND,因此它使用 & 运算符。布尔值也可以是逻辑表达式的结果。
It returns either a true or false value, much like the logical AND, but unlike the logical AND it is not short-circuited. The reason being, is that for it to preform that bitwise AND, it must know the value of both left and right operands. Here's an ex:
int x = 0;
if (false & (1 == ++x) {
System.out.println("Inside of if");
}
System.out.println(x); //"1"
Now when that if statement is ran, the expression (1 == ++x) will be executed, even though the left operand is false. Hence the value printed out for x will be 1 because it got incremented.
This also applies to Logical OR (||), bitwise OR (|), and boolean OR (|)
希望这能解释清楚一些疑惑。