Sub topic 16
Logical operators and short-circuiting
This chapter assumes you can declare Apex variables, know that null means "no value", and have used if statements and comparison operators such as == and <. Here you will combine Boolean conditions and see why the order of those conditions matters.
The three logical operators
Apex has three logical operators. They work on Boolean values.
| Operator | Name | Result | Column | Column |
|---|---|---|---|---|
| `&&` | AND | `true` only if both sides are `true` | ||
| `\ | \ | ` | OR | `true` if at least one side is `true` |
| `!` | NOT | Flips `true` to `false` and `false` to `true` |
Comparison operators produce Booleans, so you can join several comparisons into one condition.
Short-circuit evaluation
Apex evaluates && and || from left to right and stops as soon as the result is known.
- In
a && b, ifaisfalse, the result must befalse. Apex does not evaluateb. - In
a || b, ifaistrue, the result must betrue. Apex does not evaluateb.
This is called short-circuiting. It lets you put a safety check on the left and the risky work on the right.
Boolean hasValue = false;
List<Integer> items = null;
if (items != null && !items.isEmpty()) {
hasValue = true;
}
System.debug(hasValue);Here items is null, so items != null is false. Apex skips items.isEmpty(). If it ran that call, it would throw a System.NullPointerException. The debug output is false.
Boolean ready = true;
String note = null;
if (ready || note.length() > 0) {
System.debug('Skipped the null call');
}Because ready is true, the right side of || never runs. The code prints the message and throws no error.
Order matters
Put the cheaper or safer condition first. If you reverse the order in the null check above, you get a runtime error.
List<Integer> items = null;
// Wrong order: isEmpty() runs on null and throws NullPointerException
if (!items.isEmpty() && items != null) {
System.debug('never reached');
}Using NOT
! applies to the expression right after it. Use parentheses when you want to negate a whole condition.
Integer age = 20;
Boolean isMinor = !(age >= 18);
System.debug(isMinor);Without the parentheses, !age >= 18 fails to compile, because ! would apply to the Integer age.
Mixing && and ||
&& binds tighter than ||. So a || b && c means a || (b && c). Add parentheses to make your intent clear.
Boolean isAdmin = false;
Boolean isOwner = true;
Boolean isActive = false;
System.debug(isAdmin || isOwner && isActive);
System.debug((isAdmin || isOwner) && isActive);The first line prints false, because isOwner && isActive is false and isAdmin is false. The second prints false too. Change isAdmin to true to see the two results differ: the first becomes true and the second stays false.
Null Booleans
A Boolean variable can hold null. Using a null Boolean in if, &&, ||, or ! throws a NullPointerException. Test for it first.
Boolean flag = null;
if (flag != null && flag) {
System.debug('flag is true');
}
System.debug('Done');Exercise
A method receives List<Account> accounts, which may be null. Write a condition that is true only when the list has at least one element, and explain why the order of the two parts matters.
Show answer
accounts != null && !accounts.isEmpty(). Apex evaluates left to right and stops once the result is known. When accounts is null, the left side is false, so isEmpty() is never called. If you wrote !accounts.isEmpty() && accounts != null, the isEmpty() call would run first on a null list and throw a NullPointerException.
Exercise
Given Boolean a = true; Boolean b = false; Boolean c = true;, what does a || b && !c evaluate to?
Show answer
true. !c is false, b && !c is false, and a || false is true. Also, a is true, so Apex short-circuits the || and never evaluates the right side.