我一直认为Java中的&&操作符用于验证其布尔操作数是否为真,并且&操作符用于对两个整数类型进行逐位操作。

最近我知道&运算符也可以用来验证它的两个布尔操作数是否为真,唯一的区别是即使LHS操作数为假,它也会检查RHS操作数。

Java中的&操作符内部重载吗?或者这背后还有其他的概念吗?


当前回答

&是位运算符加号,用于检查两个条件,因为有时我们需要同时计算两个条件。 但是,当第一个条件为真时,逻辑运算符&&则进入第二个条件。

其他回答

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运算符和OR运算符,Java有两类求值,即Short-Circuit求值和full求值。

&& ||短路评估

短路求值使您可以不计算AND和OR表达式的右边,当整体结果可以从左边的值预测时。

int numberOne = 1;
int numberTwo = 2;
boolean result = false;

// left-side is false so the the overall result CAN be predicted without evaluating the right side.
// numberOne will be 1, numberTwo will be 2, result will be false
result = (numberOne > numberTwo) && (++numberOne == numberTwo);

System.out.println(numberOne); // prints 1
System.out.println(numberTwo); // prints 2
System.out.println(result);    // prints false


// left-side is true so the the overall result CAN NOT be predicted without evaluating the right side.
// numberOne will be 2, numberTwo will be 2, result will be true
result = (numberTwo > numberOne) && (++numberOne == numberTwo);

System.out.println(numberOne); // prints 2
System.out.println(numberTwo); // prints 2
System.out.println(result);    // prints true

^全面评估

虽然在某些情况下可以预测结果,但有必要计算右边的值。

int numberOne = 1;
int numberTwo = 2;
boolean result = false;

// left-side is false so the the overall result will be false BUT the right side MUST be evaluated too.
// numberOne will be 2, numberTwo will be 2, result will be false
result = (numberOne > numberTwo) & (++numberOne == numberTwo);

System.out.println(numberOne); // prints 2
System.out.println(numberTwo); // prints 2
System.out.println(result);    // prints false

注意:

Notice that for XOR (^) there is no short-circuit, because both sides are always required to determine the overall result. Notice that other possible names for Short-Circuit evaluation are minimal evaluation and McCarthy evaluation. It is not recommenced to mix boolean logic and actions in the same expression & can also act as a Bitwise AND operator which is very academic and can be used in cryptography. When both bits are 1, the result is 1, or either of the bits is not 1, the result is 0. (Check the following code)

AND位的例子:

byte a = 5;              // 00000101
byte b = 3;              // 00000011
byte c = (byte) (a & b); // 00000001 (c is 1)

& <——验证两个操作数 && <——如果第一个操作数求值为false,则停止求值,因为结果将为false

(x != 0) & (1/x > 1) <——这意味着求(x != 0)然后求(1/x > 1)然后执行&。问题是当x=0时,这会抛出异常。

(x != 0) && (1/x > 1) <——这意味着求(x != 0),只有当它为真时,才求(1/x > 1),所以如果你有x=0,那么这是完全安全的,并且不会抛出任何异常,如果(x != 0)求值为假,整个事情直接求值为假,而不求(1/x > 1)。

编辑:

exprA | exprB <——这意味着评估exprA然后评估exprB,然后执行|。

exprA || exprB <——这意味着评估exprA,只有当这是假的,然后评估exprB,并执行||。

对于布尔值,两者之间没有输出差异。你可以交换&&和&或||和|,它永远不会改变表达式的结果。

区别在于处理信息的场景背后。当你对a= 0和b = 1的表达式“(a != 0) & (b != 0)”进行右转时,会发生以下情况:

left side: a != 0 --> false
right side: b 1= 0 --> true
left side and right side are both true? --> false
expression returns false

当你写一个表达式(a != 0) && (b != 0)当a= 0和b = 1时,会发生以下情况:

a != 0 -->false
expression returns false

更少的步骤,更少的处理,更好的编码,特别是在处理许多布尔表达式或复杂的参数时。