Operators in Apex and Their Types
Chapter Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
Operators in Apex are essential tools that enable developers to perform various operations within their code. These operations range from simple assignments to complex logical evaluations and mathematical computations.
Understanding these operators is crucial for writing efficient and effective Apex code. This blog provides a comprehensive overview of the different types of operators in Apex, with examples to illustrate their usage.
What are Operators in Apex?
Operators are symbols that represent specific operations to be performed on one or more operands. They are vital for performing calculations, comparing values, and controlling the flow of the program. In Apex, operators are categorized into several types, each serving a unique purpose.
Types of Operators in Apex
There are various types of operators in Apex:
1. Assignment Operator
The assignment operator (=) is used to assign a value to a variable. It takes the value on the right-hand side and assigns it to the variable on the left-hand side.
2. Logical Operator
Logical operators are used to perform logical operations such as addition, subtraction, multiplication, and division.
3. Shorthand Operator
Shorthand operators combine assignment with an operation, providing a more concise syntax.
4. Relational Operator
Relational operators are used to compare two values and return a boolean result.
5. Safe navigation operator
The safe navigation operator (?.) prevents null pointer exceptions by short-circuiting expressions that attempt to operate on a null value. If the left-hand side of the expression evaluates to null, the right-hand side is not evaluated, and null is returned instead of throwing an exception.
Addition Operator
The addition operator (+) adds two values or concatenates strings.
Integer sum = x + y; // For integers or doubles Date newDate = myDate + 5; // Adds days to a date DateTime newDateTime = myDateTime + 1.5; // Adds days and fractions of a day to a datetime String fullName = firstName + ‘ ‘ + lastName; // Concatenates strings |
Subtraction Operator
The subtraction operator (-) subtracts one value from another.
Integer difference = x – y; // For integers or doubles Date newDate = myDate – 5; // Subtracts days from a date DateTime newDateTime = myDateTime – 1.5; // Subtracts days and fractions of a day from a datetime |
Add and Subtract Operator Considerations
- If X and Y are integers or doubles, then add or subtract 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 is a DateTime and Y is an integer or double. It returns a new DateTime, which is incremented or decremented by a specified number of days with a fractional portion corresponding to a portion of the day.
main() { Double d = 5.2; DateTime dt = new DateTime.now(); DateTime finalDate = dt – d ; System.debug(dt); System.debug(finalDate); } |
Note:
Only in the case of ‘+’ if X is a string and Y is a string or any other type of non-null argument, does it concatenate Y to the end of X.

Join our newsletter: Get daily update on Salesforce career insights & news!
Join Now!
Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.