Why is the output different in case of &&, &,
Here is the code segments
Can you explain why outputs are varying
1)
public static ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t && ((i++) == 0));
b = (f && ((i+=2) > 0));
System.out.println(i);
}
}
output in this case is 1
2)
public static ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t & ((i++) == 0));
b = (f & ((i+=2) > 0));
System.out.println(i);
}
}
output in this case is 3
3)
public static ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t || ((i++) == 0));
b = (f || ((i+=2) > 0));
System.out.println(i);
}
}
output in this case is 2
4)
public static ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t | ((i++) == 0));
b = (f | ((i+=2) > 0));
System.out.println(i);
}
}
output in this case is 3
Why is the output different in case of &&, &, || ?
Just as in C/C++ && is evaluated "lazily" while & is not.
If a is false then a && b will return false without even evaluating b .
Same goes for a || b a || b : If the first operand, a is true, the whole expression is true and the second operand, b is never evaluated. For a | b a | b however, both a and b will be evaluated.
This has consequences if the operand that's not being evaluated when using && (or || ) has side effects, as in your examples.
Side note: Few java-programmers know that ^ (xor) works for booleans as well. (A ^^ version does not exist simply because it would be redundant.)
There are 4 boolean binary operators that we're concerned with here:
&& is the conditional and operator & is the logical and operator || is the conditional or operator | is the logical or operator Here's the key point:
true only if both operands are true false , the result is false regardless of right operand true only if at least one operand is true true , the result is true regardless of right operand In other words, assuming no exception etc:
& and | always evaluate both operands && and || evaluate the right operand conditionally; the right operand is evaluated only if its value could affect the result of the binary operation. That means that the right operand is NOT evaluated when: && evaluates to false false ) || evaluates to true true ) References
& , ^ , and | & , the result value is true if both operand values are true ; otherwise, the result is false . | , the result value is false if both operand values are false ; otherwise, the result is true . && && operator is like & , but evaluates its right-hand operand only if the value of its left-hand operand is true . || || operator is like | , but evaluates its right-hand operand only if the value of its left-hand operand is false . See also
& , ^ , and | Related questions
| and || in Java? &= and |= short-circuit in Java? (NO!) |= ) operator in Java &&= or ||= ? && and || are the logical AND and OR operators, they always result in a boolean expression. & and | are bitwise AND and OR operators, they do a bitwise comparison of each side. For more information about what those operators do, see this link
上一篇: 相当于Delphi的Java
下一篇: &&,&为什么输出不同?
