Sub topic 10
Short-Circuit Evaluation
Java can sometimes find the result of a logical expression without checking every part. This is called short-circuit evaluation. It can save work and prevent errors.
A Boolean value is either true or false. The operators && and || combine two Boolean expressions:
&&means “and.” The result istrueonly if both sides aretrue.||means “or.” The result istrueif either side istrue, including when both aretrue.
Java checks the left side first. It checks the right side only if the result still depends on it.
When Java skips the right side
| Operator | Left side | Does Java check the right side? | Result | Column | Column |
|---|---|---|---|---|---|
| `&&` | `false` | No | `false` | ||
| `&&` | `true` | Yes | The right-side value | ||
| ` | ` | `true` | No | `true` | |
| ` | ` | `false` | Yes | The right-side value |
For example, in false && check(), the whole expression must be false. Java does not need to call check().
Guarding against `null`
A guard is a check that makes a later operation safe. In Java, null means a variable does not refer to an object. Calling a method on null causes an error. A method is a named action, such as isEmpty(), which checks whether a string has no characters.
Save this program as ShortCircuitDemo.java, then compile and run it.
public class ShortCircuitDemo {
public static boolean check() {
System.out.println("check ran");
return true;
}
public static void main(String[] args) {
String name = null;
boolean hasText = name != null && !name.isEmpty();
boolean missing = name == null || name.isEmpty();
System.out.println("hasText: " + hasText);
System.out.println("missing: " + missing);
System.out.println("false &&: " + (false && check()));
System.out.println("true ||: " + (true || check()));
System.out.println("true &&: " + (true && check()));
}
}The output is:
hasText: false
missing: true
false &&: false
true ||: true
check ran
true &&: trueIn the hasText expression, name != null is false, so Java skips !name.isEmpty(). The ! operator means “not.” If name referred to a string, Java would check whether that string was not empty.
In the missing expression, name == null is true, so Java skips name.isEmpty().
Skipped work and side effects
A side effect is a change or action that happens while an expression is checked. Printing text is one example. In the program, check() prints a line when it runs.
The first two uses of check() are skipped. The last one runs because true && still needs the right-side value. Its message appears before the final result.
Order matters for safety. Writing name.isEmpty() || name == null would try isEmpty() first and cause an error when name is null. Put the guard first.
Exercise
If name is null, what is the result of name != null && name.isEmpty()? Does Java call isEmpty()?
Show answer
The result is false. Java skips isEmpty() because the left side is already false.
Exercise
Does check() run in false || check()? Why?
Show answer
Yes. With ||, a false left side is not enough to decide the result, so Java checks the right side.