In Salesforce Apex, we have a list of standard operators that are used to perform various operations on variables, values, and expressions.
In Apex, we have a wide range of operators, such as arithmetic operators, comparison operators, logical operators, and assignment operators, that are used for specific scenarios.
In this Salesforce tutorial, we will learn about Operators in Salesforce Apex and discuss various aspects, such as syntax and use cases.
Below is the list of operators that we will learn about in this tutorial.
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
Let’s understand all the Apex operators mentioned above with the help of syntax and use case examples.
Arithmetic Operators in Salesforce Apex
In Salesforce Apex, we have arithmetic operators for performing mathematical operations, such as addition, subtraction, multiplication, and division, on values, variables, and expressions.
The list of arithmetic operators available in Apex for performing mathematical operations is provided below.
| Operator | Description |
| ‘+’ | addition |
| ‘-‘ | subtraction |
| ‘*’ | multiplication |
| ‘/’ | division |
Use Case of Arithmetic Operators in Salesforce Apex
In the code below, we will see the method to call and execute the arithmetic operators in the Salesforce Apex code. In this example, I will run the Apex code in the VS Code IDE, but you can also execute it from the Salesforce Apex anonymous window.
Integer a = 17;
Integer b = 5;
Integer sum = a + b; // Addition
Integer difference = a - b; // Subtraction
Integer multiply = a * b; // Multiplication
Integer divide = a / b; // Division
System.debug('Sum of integer a and b is: ' + sum);
System.debug('Difference between integer a and b is: ' + difference);
System.debug('Product of integer a and b is: ' + multiply);
System.debug('Quotient of integer a and b is: ' + divide); After executing the Apex anonymous code, the output tab shows the outputs of all operators for the integer values.

If you have executed the code in the Salesforce developer console, the output will be displayed in the execution log window.
This way, we can use and execute the arithmetic operators in Salesforce Apex.
Remainder Key (%) error in Salesforce Apex:
In the above code, we have not executed the arithmetic operator, modulus (%), which finds the remainder value after dividing two integers.
When I executed the code below for the modulus operator in the Apex anonymous window, I received an error message: “Found punctuation symbol or operator ‘%’ that isn’t valid in Apex.”
Apex Code:
Integer a = 17;
Integer b = 5;
Integer modulus = a % b;
System.debug('Modulus: ' + modulus);Error Message:

In Salesforce Apex, the modulus operator (%) is invalid. To get the remainder value in any math operation, we should consider using Math.Mod(X, Y); method.
Execute the code below in the Apex execution window to calculate the modulus.
integer X = 5;
integer Y = 3;
integer Remainder = Math.Mod(X,Y);
System.debug('remainder value is :' + Remainder);After executing the above code, you will see the output as the remainder value.

This way, we can get the remainder value between two digits using the Math.Mod(x,y) method in Salesforce Apex.
Comparison Operators in Salesforce Apex
In Salesforce, Comparison Operators are statements that compare values and return a Boolean value (true or false).
We use various comparison operators in Salesforce Apex.
| Operator | Description | Function |
| ‘==’ | Equals to | Checks if two values are equal. |
| ‘!=’ | Not equal to | Checks if two values are not equal |
| ‘>’ | Greater than | Checks if the first value is greater than the second |
| ‘<‘ | Less than | Checks if the first value is less than the second |
| ‘>=’ | Greater than or equal to | Checks if the first value is greater than or equal to the second. |
| ‘<=’ | Less than or equal to | Check if the first value is less than or equal to the second. |
Use case of Comparison Operators in Salesforce Apex
We will execute all the comparison operators mentioned in the above table. We will execute the operators through the apex anonymous code, where we compare the values of integers a and b using the comparison operators.
Apex Anonymous Code:
Integer a = 15;
Integer b = 10;
Boolean isEqual = a == b; // Equal to
Boolean isNotEqual = a != b; // Not equal to
Boolean isGreater = a > b; // Greater than
Boolean isLess = a < b; // Less than
Boolean isGreaterOrEqual = a >= b; // Greater than or equal to
Boolean isLessOrEqual = a <= b; // Less than or equal to
System.debug('Integer A is Equal to B: ' + isEqual);
System.debug('Integer A is Not Equal to B: ' + isNotEqual);
System.debug('Integer A is Greater than B: ' + isGreater);
System.debug('Integer A is Less than B: ' + isLess);
System.debug('Integer A is Greater or Equal to B: ' + isGreaterOrEqual);
System.debug('Integer A is Less or Equal to B: ' + isLessOrEqual);Output:

This way, you can use and execute the comparison operators in Salesforce Apex by using the method explained in the above code.
Logical Operators in Salesforce Apex
In Salesforce Apex, Logical operators are also comparison operators. They combine two or more conditions or expressions to return a Boolean value (true or false). Logical operators in Salesforce include AND, OR, and NOT.
| Operator | Name | Description |
| && | Logical AND | True if both the values are true |
| || | Logical OR | True if either of the values is true |
| ! | Logical NOT | Returns the opposite of a condition and checks if the values on either side of the operator are not equal. |
Use Case of Logical Operators in Salesforce Apex
1. AND Operator (&&): It returns the output as True when both conditions are True; otherwise, it is False.
Integer x = 10;
Integer y = 20;
Boolean result = (x > 5 && y > 15);
System.debug('AND Operator Result: ' + result);Output:

As we can see, the output is returned as True when both conditions are True in the above code.
2. OR Operator (||): It returns true when either of the conditions is true. It returns false only when both conditions are false.
Integer x = 10;
Integer y = 5;
Boolean result = (x > 5 || y > 15);
System.debug('OR Operator Result: ' + result);Output:

In the above code, the first condition was false, and the second condition was True. We got the output True because if any conditions are True, the OR operator returns the output as True.
3. NOT Operator (!): The function of the NOT operator is to check if the values on either side of the operator are not equal.
Boolean isActive = false;
Boolean result = !isActive;
System.debug('NOT Operator Result: ' + result);Output:

In the above code, we have defined the boolean isActive as False, then the NOT operator defined !isActive, and the ‘!’ operator reversed the true value to false and vice versa, so we got output as True.
This way, we can use and execute the AND, OR, and NOT logical operators in Salesforce Apex by following the above methods.
Assignment Operators in Salesforce Apex
In Salesforce Apex, the Assignment operator assigns a value to a variable, where the value on the right-hand side is assigned to the variable on the left-hand side with the help of the ‘=’ operator.
Assignment operator example:
Integer num = 10;
String name = 'Salesforce Apex';In the Assignment operator, the values of both sides should be of the same data type and should not be null.
You may also like to read:
- Polymorphism in Salesforce Apex
- Ternary Operator in Salesforce Apex
- Inheritance in Salesforce Apex
- Safe Navigation Operator in Salesforce Apex

Abhijeet is a skilled Salesforce developer with experience in developing and integrating dashboards, data reports, and Salesforce applications. He is also skilled at optimizing processes and flow automation processes, coding, and executing complex project architecture. Read more about us | LinkedIn Profile.