我一直认为Java中的&&操作符用于验证其布尔操作数是否为真,并且&操作符用于对两个整数类型进行逐位操作。
最近我知道&运算符也可以用来验证它的两个布尔操作数是否为真,唯一的区别是即使LHS操作数为假,它也会检查RHS操作数。
Java中的&操作符内部重载吗?或者这背后还有其他的概念吗?
我一直认为Java中的&&操作符用于验证其布尔操作数是否为真,并且&操作符用于对两个整数类型进行逐位操作。
最近我知道&运算符也可以用来验证它的两个布尔操作数是否为真,唯一的区别是即使LHS操作数为假,它也会检查RHS操作数。
Java中的&操作符内部重载吗?或者这背后还有其他的概念吗?
当前回答
几乎所有的答案都很好地涵盖了每一个比较点。我只想再举一个例子。以演示输出如何根据所使用的操作符而变化。考虑下面的例子
int a = 10;
if(++a==10 & ++a==12) {
++a;
}
System.out.println(a); //12
在上面的代码中,我们使用了按位&运算符。因此,它将计算两个参数(左和右),而不考虑单个结果。
所以a会在if条件内增加2倍。但是由于条件不会变为true,它不会进入if循环内部,第三个增量也不会发生。所以a的最终值在这里是12。
现在假设,在上面的同一个例子中,如果我们使用短路&&运算符。然后在将++a==10赋值为false后,它将不会去检查第二个参数。这就是未来11人的最终价值。
int a = 10;
if(++a==10 && ++a==12) {
++a;
}
System.out.println(a); //11
基于此,我们可以说,与短路&&算子相比,位&算子的性能相对较低。位运算符将对两个参数求值,而不考虑第一个参数的结果。而&&运算符将停止计算第二个参数,如果第一个参数的结果为假。
这两者之间的另一个区别是,Bitwise &操作符适用于布尔型以及整型。而短路&&运算符仅适用于布尔类型。
我们可以写
System.out.println(4 & 5); // 4
但如果我们试着这样写,
System.out.println(4 && 5);
然后它会给出一个错误,
二元运算符'&&'的错误操作数类型
其他回答
除了&&和||是短路外,在混合这两种形式时,还要考虑运算符优先级。 我认为每个人都不会立即看出result1和result2包含不同的值。
boolean a = true;
boolean b = false;
boolean c = false;
boolean result1 = a || b && c; //is true; evaluated as a || (b && c)
boolean result2 = a | b && c; //is false; evaluated as (a | b) && c
' && ': -是一个逻辑与运算符,根据其参数的逻辑关系产生一个布尔值true或false。
例如:—Condition1 && Condition2
如果Condition1为假,那么(Condition1 && Condition2)将始终为假,这就是为什么这个逻辑运算符也被称为短路运算符的原因,因为它不计算另一个条件。如果条件1为假,则不需要计算条件2。
如果条件1为真,则评估条件2,如果它为真,则总体结果为真,否则为假。
' & ': -是位与运算符。如果两个输入位都为1,则在输出中产生1(1)。否则产生0(0)。
例如:-
int = 12;// 12的二进制表示是1100
int b = 6;// 6的二进制表示是0110
Int c=(a & b);//(12 & 6)的二进制表示为0100
c是4。
如需参考,请参阅http://techno-terminal.blogspot.in/2015/11/difference-between-operator-and-operator.html
对于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)
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
我想我的答案可以更容易理解:
&和&&之间有两个区别。
如果他们使用逻辑与
&和&&可以是逻辑与,当&或&&左右表达式结果全部为真时,整个运算结果可以为真。
当&和&&作为逻辑与时,有区别:
当使用&&作为逻辑与时,如果左边的表达式结果为假,右边的表达式将不会执行。
举个例子:
String str = null;
if(str!=null && !str.equals("")){ // the right expression will not execute
}
如果使用&:
String str = null;
if(str!=null & !str.equals("")){ // the right expression will execute, and throw the NullPointerException
}
还有一个例子:
int x = 0;
int y = 2;
if(x==0 & ++y>2){
System.out.print(“y=”+y); // print is: y=3
}
int x = 0;
int y = 2;
if(x==0 && ++y>2){
System.out.print(“y=”+y); // print is: y=2
}
&可以用作位运算符
&可以用作位与运算符,&&不能。
按位AND“&”运算符当且仅当两个位都在时产生1 它的操作数是1。然而,如果两个比特都是0,或者两个比特都不相同,那么这个运算符产生0。更精确地说,“&”运算符如果两位中的任何一位为1,则返回1,如果其中任何一位为0,则返回0。
来自维基页面:
http://www.roseindia.net/java/master-java/java-bitwise-and.shtml