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 must be Integers or Doubles or a combination.

Note

x and y can’t be null.
-= 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 must be Integers or Doubles or a combination.

Note

x and y can’t be null.
/= 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.

Note

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.

Note

&& 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.
Note:
  • && has precedence over ||
  • This operator exhibits short-circuiting behavior, which means y is evaluated only if x is true.
  • x and y can’t be null.
|| 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.
Note:
  • && has precedence over ||
  • This operator exhibits short-circuiting behavior, which means y is evaluated only if x is false.
  • x and y can’t be null.
== 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.
  • Unlike Java, == in Apex compares object value equality not reference equality, except for user-defined types. Therefore:
  • For sObjects and sObject arrays, == performs a deep check of all sObject field values before returning its result. Likewise for collections and built-in Apex objects.
  • For records, every field must have the same value for == to evaluate to true.
  • x or y can be the literal null.
  • The comparison of any two values can never result in null.
  • SOQL and SOSL use = for their equality operator and not ==. Although Apex and SOQL and SOSL are strongly linked, this unfortunate syntax discrepancy exists because most modern languages use = for assignment and == for equality. The designers of Apex deemed it more valuable to maintain this paradigm than to force developers to learn a new assignment operator. As a result, Apex developers must use == for equality tests in the main body of the Apex code, and = for equality in SOQL and SOSL queries.
  • String comparison using == is case-insensitive and is performed according to the locale of the context user
  • ID comparison using == is case-sensitive and doesn’t distinguish between 15-character and 18-character formats
  • User-defined types are compared by reference, which means that two objects are equal only if they reference the same location in memory. You can override this default comparison behavior by providing equals and hashCode methods in your class to compare object values instead.

Note

=== 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.
  • Unlike other database stored procedures, Apex doesn’t support tri-state Boolean logic and the comparison of any two values can never result in null.
  • If x or y equal null and are Integers, Doubles, Dates, or Datetimes, the expression is false.
  • A non-null String or ID value is always greater than a null value.
  • If x and y are IDs, they must reference the same type of object. Otherwise a runtime error results.
  • If x or y is an ID and the other value is a String, the String value is validated and treated as an ID.
  • x and y can’t be Booleans.
  • The comparison of two strings is performed according to the locale of the context user and is case-insensitive.

Note

> x > y Greater than operator. If x is greater than y, the expression evaluates to true. Otherwise the expression evaluates to false.
  • The comparison of any two values can never result in null.
  • If x or y equal null and are Integers, Doubles, Dates, or Datetimes, the expression is false.
  • A non-null String or ID value is always greater than a null value.
  • If x and y are IDs, they must reference the same type of object. Otherwise a runtime error results.
  • If x or y is an ID and the other value is a String, the String value is validated and treated as an ID.
  • x and y can’t be Booleans.
  • The comparison of two strings is performed according to the locale of the context user and is case-insensitive.

Note

<= 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.
  • The comparison of any two values can never result in null.
  • If x or y equal null and are Integers, Doubles, Dates, or Datetimes, the expression is false.
  • A non-null String or ID value is always greater than a null value.
  • If x and y are IDs, they must reference the same type of object. Otherwise a runtime error results.
  • If x or y is an ID and the other value is a String, the String value is validated and treated as an ID.
  • x and y can’t be Booleans.
  • The comparison of two strings is performed according to the locale of the context user and is case-insensitive.

Note

>= 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.
  • The comparison of any two values can never result in null.
  • If x or y equal null and are Integers, Doubles, Dates, or Datetimes, the expression is false.
  • A non-null String or ID value is always greater than a null value.
  • If x and y are IDs, they must reference the same type of object. Otherwise a runtime error results.
  • If x or y is an ID and the other value is a String, the String value is validated and treated as an ID.
  • x and y can’t be Booleans.
  • The comparison of two strings is performed according to the locale of the context user and is case-insensitive.

Note

!= 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.
  • String comparison using != is case-insensitive
  • Unlike Java, != in Apex compares object value equality not reference equality, except for user-defined types.
  • For sObjects and sObject arrays, != performs a deep check of all sObject field values before returning its result.
  • For records, != evaluates to true if the records have different values for any field.
  • User-defined types are compared by reference, which means that two objects are different only if they reference different locations in memory. You can override this default comparison behavior by providing equals and hashCode methods in your class to compare object values instead.
  • x or y can be the literal null.
  • The comparison of any two values can never result in null.

Note

!== 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:
  • If x and y are Integers or Doubles, the operator adds the value of x to the value of y. If a Double is used, the result is a Double.
  • If x is a Date and y is an Integer, returns a new Date that is incremented by the specified number of days.
  • If x is a Datetime and y is an Integer or Double, returns a new Date that is incremented by the specified number of days, with the fractional portion corresponding to a portion of a day.
  • If x is a String and y is a String or any other type of non-null argument, concatenates y to the end of x.
- x - y Subtraction operator. Subtracts the value of y from the value of x according to the following rules:
  • If x and y are Integers or Doubles, the operator subtracts the value of y from the value of x. If a Double is used, the result is a Double.
  • If x is a Date and y is an Integer, returns a new Date that is decremented by the specified number of days.
  • If x is a Datetime and y is an Integer or Double, returns a new Date that is decremented by the specified number of days, with the fractional portion corresponding to a portion of a day.
* 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.