Understanding Expression Operators

Expressions can also 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. Note that the data type of x must match the data type of y, and cannot 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 cannot 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. Note that x and y must be Integers or Doubles, or a combination. x and y cannot 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. Note that x and y must be Integers or Doubles, or a combination. x and y cannot 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. Note that x and y must be Integers or Doubles, or a combination. x and y cannot 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.
Note:
  • This operator exhibits “short-circuiting” behavior, which means y is evaluated only if x is false.
  • x and y cannot 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.
Note:
  • This operator exhibits “short-circuiting” behavior, which means y is evaluated only if x is true.
  • x and y cannot 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. Note that x cannot 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.
Note:
  • && has precedence over ||
  • This operator exhibits “short-circuiting” behavior, which means y is evaluated only if x is true.
  • x and y cannot 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 cannot 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.
Note:
  • Unlike Java, == in Apex compares object value equality, not reference equality, except for user-defined types. Consequently:
    • String comparison using == is case-insensitive
    • ID comparison using == is case-sensitive, and does not 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.
  • 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. The result is that Apex developers must use == for equality tests in the main body of the Apex code, and = for equality in SOQL and SOSL queries.
=== 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.
Note:
  • Unlike other database stored procedures, Apex does not 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 cannot be Booleans.
  • The comparison of two strings is performed according to the locale of the context user and is case-insensitive.
> x > y Greater than operator. If x is greater than y, the expression evaluates to true. Otherwise, the expression evaluates to false.
Note:
  • 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 cannot be Booleans.
  • The comparison of two strings is performed according to the locale of the context user and is case-insensitive.
<= 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.
Note:
  • 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 cannot be Booleans.
  • The comparison of two strings is performed according to the locale of the context user and is case-insensitive.
>= 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.
Note:
  • 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 cannot be Booleans.
  • The comparison of two strings is performed according to the locale of the context user and is case-insensitive.
!= x != y Inequality operator. If the value of x does not equal the value of y, the expression evaluates to true. Otherwise, the expression evaluates to false.
Note:
  • 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.
!== x !== y Exact inequality operator. If x and y do not 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, 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, 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. Note that 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. Note that 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. Note that the positive equivalent + is also syntactically valid, but does not 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. This operator is not valid for types Long or Integer.
| 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. This operator is not valid for types Long or Integer.
^ 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) Parentheses. Elevates the precedence of an expression x so that it is evaluated first in a compound expression.
Previous
Next