Sub topic 13
Arithmetic operators
This chapter assumes you can declare Apex variables and know the numeric types Integer, Long, Double, and Decimal. It covers what each arithmetic operator does, what type the result has, and the errors you are most likely to hit.
The operators
| Operator | Meaning | Notes |
|---|---|---|
| `+` | Addition | Also joins Strings |
| `-` | Subtraction | Unary form negates a value |
| `*` | Multiplication | |
| `/` | Division | Throws on divide by zero |
| `++` / `--` | Increment / decrement | Prefix or postfix |
Apex has no % operator. Use Math.mod() to get a remainder.
Integer a = 10;
Integer b = 3;
System.debug(a + b);
System.debug(a - b);
System.debug(a * b);
System.debug(Math.mod(a, b));
System.debug(-a);The output is 13, 7, 30, 1, and -10.
Result types
When the operands have different types, Apex converts the narrower one to the wider one. The result has the wider type.
| Left | Right | Result |
|---|---|---|
| Integer | Long | Long |
| Integer or Long | Decimal | Decimal |
| Integer, Long, or Decimal | Double | Double |
Choose the type before you calculate. Integer holds 32-bit values, so a large product can overflow. Declare one operand as Long if the result may be large.
Exact division with Decimal
Division often produces repeating digits. For money, use Decimal.divide(). It takes the divisor, the number of decimal places, and a rounding mode.
Decimal total = 10;
Decimal share = total.divide(3, 2, RoundingMode.HALF_UP);
System.debug(share);This prints 3.33. Rounding explicitly keeps the scale predictable.
Increment and decrement
++ and -- change a variable by 1. The prefix form changes the value before the expression uses it. The postfix form changes it afterward.
Integer i = 5;
Integer x = i++;
Integer y = ++i;
System.debug(x + ' ' + y + ' ' + i);This prints 5 7 7. In the first line, x receives 5 and then i becomes 6. In the second line, i becomes 7 before y receives it.
Common errors
Two runtime errors come up most often.
- Null operands. Arithmetic on a null value throws a
NullPointerException. - Division by zero. This throws a
MathException.
Integer count;
Integer zero = 0;
try {
Integer next = count + 1;
} catch (NullPointerException e) {
System.debug('count is null');
}
try {
Decimal r = 5 / zero;
} catch (MathException e) {
System.debug(e.getMessage());
}Adding strings
If either operand of + is a String, Apex joins the values as text instead of adding them. Evaluation runs left to right, so 1 + 2 + 'x' gives '3x', but 'x' + 1 + 2 gives 'x12'. Add parentheses when you mix numbers and text.
Exercise
A variable Decimal fee; is declared but never assigned. What happens when you run Decimal total = fee * 2;, and how can you avoid it?
Show answer
The variable is null, so the multiplication throws a NullPointerException. Initialize it (Decimal fee = 0;) or check for null before you calculate.
Exercise
Write an expression that splits Decimal amount = 100; into 3 equal parts, rounded to 2 decimal places using half-up rounding. What value does it give?
Show answer
amount.divide(3, 2, RoundingMode.HALF_UP) gives 33.33.