Sub topic 18
Operator precedence and grouping
This chapter assumes you know the arithmetic, comparison, and logical operators from the earlier chapters. It covers the order Apex evaluates them in, how parentheses change that order, and the bitwise operators.
How precedence works
When an expression has several operators, Apex does not simply read left to right. Each operator has a precedence level. Higher levels bind first. When two operators share a level, associativity decides the order. Most binary operators are left-associative, so they evaluate from left to right.
The table lists the groups you use most, from highest to lowest.
| Level | Operators | Notes | Column | Column |
|---|---|---|---|---|
| 1 | `()` `++` `--` (postfix) | Grouping binds tightest | ||
| 2 | `!` unary `-` `+` `(Type)` cast | Right to left | ||
| 3 | `*` `/` | Left to right | ||
| 4 | `+` `-` | Left to right | ||
| 5 | `<` `<=` `>` `>=` | Comparison | ||
| 6 | `==` `!=` | Equality | ||
| 7 | `&&` | Evaluated before `\ | \ | ` |
| 8 | `\ | \ | ` | |
| 9 | `=` and compound assignment | Right to left |
The bitwise and shift operators also sit between the arithmetic and logical levels. Their exact ranks are easy to misremember, so use parentheses whenever you mix them with other operators.
Integer a = 2 + 3 * 4;
Integer b = (2 + 3) * 4;
Integer c = 10 - 4 - 3;
Boolean d = true || false && false;
Boolean e = (true || false) && false;
System.debug(a + ' ' + b + ' ' + c);
System.debug(d + ' ' + e);The first line prints 14 20 3. Multiplication runs before addition. Subtraction is left-associative, so 10 - 4 - 3 means (10 - 4) - 3. The second line prints true false. Because && binds tighter than ||, d is true || (false && false).
Group to show intent
Parentheses cost nothing at runtime. They also tell the next reader what you meant. Compare these two lines:
Boolean ready = true;
Boolean ok = 1 + 2 * 3 > 6 && ready;
Boolean clear = ((1 + (2 * 3)) > 6) && ready;
System.debug(ok == clear);Both lines give the same result, and the output is true. The second line is the one a teammate can read without checking the table.
Bitwise operators
Bitwise operators work on the binary digits of Integer and Long values. They are rare in Apex. You see them mostly in flag fields and low-level encoding.
| Operator | Meaning | Column |
|---|---|---|
| `&` | AND: a bit is 1 only if both bits are 1 | |
| `\ | ` | OR: a bit is 1 if either bit is 1 |
| `^` | XOR: a bit is 1 if the bits differ | |
| `<<` | Shift left | |
| `>>` | Shift right, keeping the sign | |
| `>>>` | Shift right, filling with zeros |
Apex has no bitwise NOT operator (~). The common use is packing several yes/no flags into one number.
Integer READ = 1;
Integer WRITE = 2;
Integer EXEC = 4;
Integer perms = READ | WRITE;
Boolean canWrite = (perms & WRITE) != 0;
Integer toggled = perms ^ WRITE;
System.debug(perms);
System.debug(canWrite);
System.debug(toggled);
System.debug(1 << 3);This prints 3, true, 1, and 8. The parentheses around perms & WRITE matter. Without them, != can bind first and give a different result.
Exercise
What does Integer x = 6 & 3 | 8; produce if & is evaluated before |, and how would you write the expression so the order is explicit?
Show answer
6 & 3 is 110 & 011 = 010, which is 2. Then 2 | 8 is 0010 | 1000 = 1010, which is 10. Write it as (6 & 3) | 8.