Edd*_*e B 4 java evaluation logical-operators
可能重复:
&和&&的差异
我已经阅读了几篇有关java中短路操作的教程和答案,我仍然不太了解java处理双垂直管道与双安培管道短路的区别.例如 ...
为什么逻辑AND短路评估失败?
引用JSL 15.23.条件和运算符&&
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.
public static void main( String... args ) {
int a = 1;
int b = 2;
// Okay. Prints
if( a == 1 | b == 3 ) {
System.out.println( "Comparison via |" + "\na is " + a + "\nb is " + b );
}
// Okay. Prints
if( a == 1 || b == 3 ) {
System.out.println( "Comparison via ||" + "\na is " + a + "\nb is " + b );
}
// Okay. Does not print
if( a == 1 & b == 3 ) {
System.out.println( "Comparison via &" + "\na is " + a + "\nb is " + b );
}
// I don't understand. Shouldn't the Short Circuit succeed since the left side of the equation equals 1?
if( a == 1 && b == 3 ) {
System.out.println( "Comparison via &&" + "\na is " + a + "\nb is " + b );
}
}
Run Code Online (Sandbox Code Playgroud)