Salesforce with Ankit

Salesforce beyond the clouds !

Operators

In this article we will go through all operators, we use in Apex. We have categorized all operators into five types, we will take overview of each and see some examples.

  1. Comparison operators –
    • Equality ( == ) :
      • The double equal symbol compares whether two values are equal or not.
      • This operator is boolean & will return true if values from both sides of operator are equal and return false if they are unequal.
    • Inequality ( != ) :
      • Inequality compares whether two values are different or not.
      • Like equality, this operator is also boolean and returns true if values from both side of operator are different, else it will return false.
    • Greater Than ( > ) :
      • This operator compares two values and returns true if the left-hand side value is larger than the right hand side value.
    • Greater Than or Equal to ( >= ):
      • Similar to grater than, this will return true if the left-hand side value is greater than or equal to right-hand side value.
    • Less Than ( < ) :
      • This will return true if left-hand side is smaller than right hand side.
    • Less Than or Equal to ( <= ) :
      • This will return true if the left-hand side value is smaller or equal to right-hand side value.
  2. Logical Operators –
    • AND Operator ( && ) :
      • This operator will return true if both values ( value of right and left hand side of operator) are true, else it will return false.
      • If left-hand side value is false, it will not calculate right-hand side & return false.
    • OR Operator ( || ) :
      • OR Operator will return true if ant of left or right-hand side value is true. This will return false if both values are false.
    • NOT Operator ( ! ) [Logical Complement] :
      • NOT Operator returns inverse value for the boolean variable.
  3. Assignment Operators –
    • Addition Assignment ( += ) :
      • The addition assignment operator will take the value on the right-hand side of the operator and add it to left hand side before assigning new total.
      • For Example:
        • Integer p = 5; ( Value if p is 5 )
        • p += 7; ( Addition Assignment )
        • New Value of p is 12.
      • If variable is of type String, we can append other string onto end.
      • For Example:
        • String greetings = ‘Hello’;
        • greetings += ‘World!’;;
        • New value of greetings will be ‘HelloWorld!’.
    • Subtraction Assignment ( -= ):
      • Similar to addition operator, but subtracts instead of addition on left-side.
      • For Example:
        • Integer t = 12;
        • t -= 4;
        • New value of t will be 8.
    • Multiplicative Assignment ( *= ) :
      • As like previous assignment operators, multiplicative assignment operator works in same way, and multiplies left-hand and right-hand side values before assigning them to left-hand side.
      • For Example:
        • Integer k = 13 ;
        • k *= 9;
        • New value of the k will be 117.
    • Divisive Assignment ( /= ) :
      • Divisive assignment operator divides left-hand side by right-hand side before storing new value in left-hand side.
      • For Example:
        • Integer c = 343;
        • c /= 7;
        • New value of c will be 49.
  4. Action Operators –
    • Addition Operator ( + ) :
      • The addition operator take two values and adds them together based upon a number of rules dependent on data-type.
      • For Example:
        • Integer m = 12 + 19; (Addition will take place and m will be 31).
        • Double n = 0.125 + 0.625 ( After addition n will be 0.75).
      • If first value is date and second value is Integer, date will be increased by value of integer.
      • If first value is Datetime and the second value is an Integer or double, the date will be incremented by the number. ( With any decimal portion will be added as a portion of the day. )
      • If two values are strings, the value after addition will be concatenated string.
      • For Example:
        • String message = ‘Hi!’ + ‘Siri’;
        • Value stored in message will be ‘Hi!Siri’.
    • Subtraction Operator ( – ):
      • Subtraction operator subtracts right-hand side value from left-hand side value.
      • For Example:
        • Integer j = 196 – 180;
        • Value stored in j will be 16.
      • For first operator being date and second being integer, result will be number of days are reduced from date value.
      • Like addition, for Datetime & Integer or Double, number of days are deducted from the date value. ( Portion of the day in case of double value).
      • There is no usage of subtraction operator on String.
    • Multiplication Operator ( * ) :
      • Multiplication operator multiplies two values together. (Must be either integer or double) and return result.
      • For example:
        • Integer value = 25 * 20 ;
        • value will store 500.
    • Division Operator ( / ) :
      • Division operator divides first value by second value.
      • For Example:
        • Integer cr = 729 / 9;
        • cr will have value of 81.
    • Increment Operator ( ++ ) :
      • Increment operator will increase the value of variable by one.
      • This is very helpful in looping.
      • Pre-increment ( ++x ), post-increment ( x++ ).
    • Decrement Operator ( — ) :
      • Decrement operator will decrease the value of variable by one.
      • This is also very helpful in looping.
      • Pre-decrement ( –x ), post-decrement ( x– ).
    • Unary Negation Operator ( – ) :
      • Unary negation operator, multiplies value of variable by -1.
      • For Example:
        • Integer h = 8;
        • p = -h;
        • Value of p will be -8.

Thank you for reading the article till the end. In next article we will discuss about control statements and loops.

Published by

Leave a comment