Sub topic 17
Ternary, ?., and ?? operators
This chapter assumes you know how to declare variables, how null behaves in Apex, and how if statements and comparison operators work. Three operators help you pick a value or avoid a null reference error without writing a full if block: the ternary operator, safe navigation (?.), and null coalescing (??).
The ternary operator
The ternary operator condition ? valueIfTrue : valueIfFalse evaluates a Boolean condition and returns one of two values. Only the chosen branch is evaluated.
Integer score = 72;
String grade = score >= 60 ? 'Pass' : 'Fail';
System.debug(grade);Both branches should produce compatible types, because the result is assigned to one variable. Use it for short choices. Nested ternaries get hard to read, so switch to if/else when a second condition appears.
Safe navigation: ?.
Reading a field or calling a method on a null reference throws a System.NullPointerException. The safe navigation operator ?. stops the chain when the left side is null and returns null instead of throwing.
Account acc = null;
String accountName = acc?.Name;
System.debug(accountName == null);Here acc?.Name evaluates to null. It works on method calls and can be chained, for example acc?.Owner?.Name. If any link is null, the whole expression is null.
The result of a ?. expression can be null, so the variable that receives it must be able to hold null. Use a wrapper-friendly type such as String or Integer, and test the result before you use it.
Null coalescing: ??
The ?? operator returns its left operand if that operand is not null. Otherwise it returns the right operand. It supplies a default value.
String accountName = null;
String label = accountName ?? 'Unknown';
System.debug(label);?? only checks for null. An empty string '' is not null, so '' ?? 'Unknown' returns ''.
Combining the operators
?. and ?? work well together. Safe navigation produces null when something is missing, and coalescing replaces that null with a default.
Integer score = 72;
String grade = score >= 60 ? 'Pass' : 'Fail';
Account acc = null;
String accountName = acc?.Name;
String label = accountName ?? 'Unknown';
System.debug(grade + ' ' + label);The output is Pass Unknown. You can also write acc?.Name ?? 'Unknown' in one line.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| Using `acc.Name` when `acc` may be `null` | `NullPointerException` | Use `acc?.Name` |
| Expecting `??` to replace an empty string | The empty string is kept | Test with `String.isBlank()` |
| Using `?.` and assigning to a primitive such as `Integer` | Possible null assignment error at run time | Check for `null` or add `?? 0` |
| Deeply nested ternaries | Code is hard to read | Use `if`/`else` |
Exercise
What does this code print? Account acc = null; String name = acc?.Name ?? 'None'; System.debug(name);
Show answer
It prints None. The expression acc?.Name evaluates to null because acc is null, so ?? returns the right operand, 'None'.
Exercise
Rewrite this code using one ?? operator and no if statement. String city = c.City; if (city == null) { city = 'N/A'; }
Show answer
String city = c.City ?? 'N/A';