Expression Operators
Expressions can be joined to one another with operators to create compound expressions.
Apex supports the following operators:
| Operator | Syntax | Description |
|---|---|---|
= | x = y | Assignment operator (Right associative). Assigns the value of y to the L-value x. The data type of x must match the data type of y and can’t be null. |
+= | x += y | Addition assignment operator (Right associative). Adds the value of y to the original value of x and then reassigns the new value to x. See + for additional information. x and y can’t be null. |
*= | x *= y | Multiplication assignment operator (Right associative). Multiplies the value of y with the original value of x and then reassigns the new value to x. x and y can’t be null.x and y must be Integers or Doubles or a combination. |
-= | x -= y | Subtraction assignment operator (Right associative). Subtracts the value of y from the original value of x and then reassigns the new value to x. x and y can’t be null.x and y must be Integers or Doubles or a combination. |
/= | x /= y | Division assignment operator (Right associative). Divides the original value of x with the value of y and then reassigns the new value to x. x and y must be Integers or Doubles or a combination.x and y can’t be null. |
|= | x |= y | OR assignment operator (Right associative). If x, a Boolean, and y, a Boolean, are both false, then x remains false. Otherwise x is assigned the value of true. x and y can’t be null. |
&= | x &= y | AND assignment operator (Right associative). If x, a Boolean, and y, a Boolean, are both true, then x remains true. Otherwise x is assigned the value of false. x and y can’t be null. |
<<= | x <<= y | Bitwise shift left assignment operator. Shifts each bit in x to the left by y bits so that the high-order bits are lost and the new right bits are set to 0. This value is then reassigned to x. |
>>= | x >>= y | Bitwise shift right signed assignment operator. Shifts each bit in x to the right by y bits so that the low-order bits are lost and the new left bits are set to 0 for positive values of y and 1 for negative values of y. This value is then reassigned to x. |
>>>= | x >>>= y | Bitwise shift right unsigned assignment operator. Shifts each bit in x to the right by y bits so that the low-order bits are lost and the new left bits are set to 0 for all values of y. This value is then reassigned to x. |
? : | x ? y : z | Ternary operator (Right associative). This operator acts as a short-hand for if-then-else statements. If x, a Boolean, is true, y is the result. Otherwise z is the result. x can’t be null. |
&& | x && y | AND logical operator (Left associative). If x, a Boolean, and y, a Boolean, are both true, then the expression evaluates to true. Otherwise the expression evaluates to false.
|
|| | x || y | OR logical operator (Left associative). If x, a Boolean, and y, a Boolean, are both false, then the expression evaluates to false. Otherwise the expression evaluates to true.
|
== | x == y | Equality operator. If the value of x equals the value of y, the expression evaluates to true. Otherwise the expression evaluates to false.
|
=== | x === y | Exact equality operator. If x and y reference the exact same location in memory the expression evaluates to true. Otherwise the expression evaluates to false. |
< | x < y | Less than operator. If x is less than y, the expression evaluates to true. Otherwise the expression evaluates to false.
|
> | x > y | Greater than operator. If x is greater than y, the expression evaluates to true. Otherwise the expression evaluates to false.
|
<= | x <= y | Less than or equal to operator. If x is less than or equal to y, the expression evaluates to true. Otherwise the expression evaluates to false.
|
>= | x >= y | Greater than or equal to operator. If x is greater than or equal to y, the expression evaluates to true. Otherwise the expression evaluates to false.
|
!= | x != y | Inequality operator. If the value of x doesn’t equal the value of y, the expression evaluates to true. Otherwise the expression evaluates to false.
|
!== | x !== y | Exact inequality operator. If x and y don’t reference the exact same location in memory, the expression evaluates to true. Otherwise the expression evaluates to false. |
+ | x + y | Addition operator. Adds the value of x to the value of y according to the following rules:
|
- | x - y | Subtraction operator. Subtracts the value of y from the value of x according to the following rules:
|
* | x * y | Multiplication operator. Multiplies x, an Integer or Double, with y, another Integer or Double. If a double is used, the result is a Double. |
/ | x / y | Division operator. Divides x, an Integer or Double, by y, another Integer or Double. If a double is used, the result is a Double. |
! | !x | Logical complement operator. Inverts the value of a Boolean so that true becomes false and false becomes true. |
- | -x | Unary negation operator. Multiplies the value of x, an Integer or Double, by -1. The positive equivalent + is also syntactically valid but doesn’t have a mathematical effect. |
++ | x++++x | Increment operator. Adds 1 to the value of x, a variable of a numeric type. If prefixed (++x), the expression evaluates to the value of x after the increment. If postfixed (x++), the expression evaluates to the value of x before the increment. |
-- | x----x | Decrement operator. Subtracts 1 from the value of x, a variable of a numeric type. If prefixed (--x), the expression evaluates to the value of x after the decrement. If postfixed (x--), the expression evaluates to the value of x before the decrement. |
& | x & y | Bitwise AND operator. ANDs each bit in x with the corresponding bit in y so that the result bit is set to 1 if both of the bits are set to 1. |
| | x | y | Bitwise OR operator. ORs each bit in x with the corresponding bit in y so that the result bit is set to 1 if at least one of the bits is set to 1. |
^ | x ^ y | Bitwise exclusive OR operator. Exclusive ORs each bit in x with the corresponding bit in y so that the result bit is set to 1 if exactly one of the bits is set to 1 and the other bit is set to 0. |
^= | x ^= y | Bitwise exclusive OR operator. Exclusive ORs each bit in x with the corresponding bit in y so that the result bit is set to 1 if exactly one of the bits is set to 1 and the other bit is set to 0. Assigns the result of the exclusive OR operation to x. |
<< | x << y | Bitwise shift left operator. Shifts each bit in x to the left by y bits so that the high-order bits are lost and the new right bits are set to 0. |
>> | x >> y | Bitwise shift right signed operator. Shifts each bit in x to the right by y bits so that the low-order bits are lost and the new left bits are set to 0 for positive values of y and 1 for negative values of y. |
>>> | x >>> y | Bitwise shift right unsigned operator. Shifts each bit in x to the right by y bits so that the low-order bits are lost and the new left bits are set to 0 for all values of y. |
~ | ~x | Bitwise Not or Complement operator. Toggles each binary digit of x, converting 0 to 1 and 1 to 0. Boolean values are converted from True to False and vice versa. |
() | (x) | Parentheses. Elevates the precedence of an expression x so that it’s evaluated first in a compound expression. |
?. | x?.y | Safe navigation operator. Short-circuits expressions that attempt to operate on a null value, and returns null instead of throwing a NullPointerException. If the left-hand side of the chain expression evaluates to null, the right-hand side of the chain expression isn’t evaluated. |