if (0 == 1 && 2 + 2 == 4) { out.println("This line won't be printed."); }
You might expect Java to ask itself if 0 equals 1, and then ask if 2 + 2 equals 4. But with Java's && operator, that's not what happens. Instead, Java does the following:
Evaluate 0 == 1, discovering that 0 == 1 is false.
Realize that the condition (0 == 1 && whatever) can't possibly be true, no matter what the whatever condition happens to be.
Return false (without bothering to check if 2 + 2 == 4).
The condition (0 == 1 && whatever) has to be false, because 0 == 1 is false. (Remember, the && operator wants both conditions, on its left and right sides, to be true.)
So when Java finds the value on the left side of an && operator to be false, then Java gives up and declares the entire expression to be false. That's called short circuit expression evaluation. The same kind of thing happens with the || operator (another short circuit operator) when the value on the operator's left side is true.
if (2 + 2 == 4 || 0 == 1) { out.println("This line will be printed."); }Here's how Java's || operator behaves when it encounters this code: Evaluate 2 + 2 == 4, discovering that 2 + 2 == 4 is true.
Realize that the condition (2 + 2 == 4 || whatever) must be true, no matter what the whatever condition happens to be.
Return true (without bothering to check if 0 == 1).
The condition (2 + 2 == 4 || whatever) has to be true, because 2 + 2 == 4 is true. (Remember, the || operator wants either condition, on its left or right side or on both sides, to be true.)
So when Java finds the value on the left side of an || operator to be true, then Java declares the entire expression to be true.
Java's && and || operators use short circuit evaluation. Java's & and | operators also test for the "and" and "or" conditions, but these & and | operators don't do short circuit evaluation. In other words, when Java encounters the following code, Java checks to see if 0 == 1 is true and then, before giving its final answer, checks to see if 2 + 2 == 4 is true.
import static java.lang.System.out; public class OperatorEvalDemo { public static void main(String args[]) { new OperatorEvalDemo(); } OperatorEvalDemo() { if (0 == 1 && 2 + 2 == 4) { out.println("(0 == 1 && 2 + 2 == 4) is true"); } else { out.println("(0 == 1 && 2 + 2 == 4) is false"); } out.println(); if (2 + 2 == 4 || 0 == 1) { out.println("(2 + 2 == 4 || 0 == 1) is true"); } else { out.println("(2 + 2 == 4 || 0 == 1) is false"); } out.println(); if (isFalse() && isTrue()) { out.println("(isFalse() && isTrue()) is true"); } else { out.println("(isFalse() && isTrue()) is false"); } out.println(); if (isFalse() & isTrue()) { out.println("(isFalse() & isTrue()) is true"); } else { out.println("(isFalse() & isTrue()) is false"); } out.println(); if (isTrue() || isFalse()) { out.println("(isTrue() || isFalse()) is true"); } else { out.println("(isTrue() || isFalse()) is false"); } out.println(); if (isTrue() | isFalse()) { out.println("(isTrue() | isFalse()) is true"); } else { out.println("(isTrue() | isFalse()) is false"); } } boolean isTrue() { out.println("Executing isTrue"); return true; } boolean isFalse() { out.println("Executing isFalse"); return false; } }Output :
(0 == 1 && 2 + 2 == 4) is false
(2 + 2 == 4 || 0 == 1) is true
Executing isFalse
(isFalse() && isTrue()) is false
Executing isFalse
Executing isTrue
(isFalse() & isTrue()) is false
Executing isTrue
(isTrue() || isFalse()) is true
Executing isTrue
Executing isFalse
(isTrue() | isFalse()) is truea
Notice, for example, what happens with the && operator. Java displays Executing isFalse. But then Java doesn't display Executing isTrue because the && operator does short circuit evaluation. On the other hand, Java displays both Executing isFalse and Executing isTrue for the & operator, because the & operator doesn't do short circuit evaluation.