Types of Operators

1. Add/Sub:

  • If X and Y are integers or doubles then adds or subtracts the value of Y from X and if any X or Y is double then the result is double.
  • If X is a date and Y is an integer then it returns a new date that is incremented or decremented by the specified number of days.
  • If X s a DateTime and Y is an integer or double then it returns a new DateTime which is incremented or decremented by specified number of days with a fractional portion corresponding to a portion of the day.

Ex: main() {

Double d = 5.2;

DateTime dt = new DateTime.now();

DateTime finalDate = dt -d ;

System.debug(dt);

System.debug(finalDate);

}

Note: Only in case of ‘+’ if X is a string and Y is a string or any other type of non-null argument then it concatenates Y to the end of X.

2. Shorthand Operator:

  • |= (OR assignment operator):

If X(boolean) and Y(boolean) are both false then X remains false otherwise X is assigned true.

  • &= (AND assignment operator):

If X(boolean) and Y(boolean) are both true then X remains true otherwise X is assigned false.

3. Equality Operator(==):

If a value of X equals Y, the expression evaluates to true otherwise the expression evaluates to false.

Note: Unlike java(==), in apex it compares object value equality not reference equality except for user-defined types..

Note: String comparison using(==) is case insensitive.

Note: ID comparison using == is case sensitive.

  • User-defined types are compared by reference which means that the 2 objects are equal if they reference the same location in the memory.
  • You can override this default comparison behavior by equals() and hashcode() methods in your class to compare object values instead.

main()

{

Integer a[] = new Integer[5];

Integer b[] = new Integer[5];

for(Integer i=0; i<5; i++)

a[i]=i+1;

System.debug(a);

System.debug(b);

System.debug(a==b);

}

Note: Arrays(==) performs a deep check of all the values before returning its result. Likewise for collection and built-in apex objects.

Note: The comparison of any two values can never result in null though X and Y can be literal null.

Note: SOQL and SOSL use ‘=’ for their equality operator not ‘==’.

4. Exact Equality Operator(===):

If X and Y reference the exact same location in memory, the expression evaluates to true otherwise false.

main()

{

FirstClass fc = new FirstClass();

FirstClass fc2 = new FirstClass();

System.debug(if(fc===fc2));

}

5. Exact Inequality Operator(!==):

If X and Y do not reference the exact same location in emory, the expression evaluates to true otherwise false.

6. Relational Operators(<,>,<=,>=):

  • If X or Y equals null and are integers, doubles, dates or DateTimes then it will return false.
  • A non-null string or id value is always greater than a null value.
  • If X and Y are ids then, they must reference the same value otherwise a runtime error occurs.
  • If X or Y is an id and other value is a sting then the string is validated and treated as an id.
  • X and Y can not be boolean.

Id i1 = “——”;

Id i2 = “——”;

System.debug(i1>i2);

7. Operator Precedence:

  • () {} ++ —
  • -x ! +x
  • */
  • +-
  • < > <= >= instanceOf
  • == !=
  • &&
  • ||
  • += == *=

Our Recent Blog

Share This Post