Curriculum
Operators and Expressions are fundamental building blocks of Apex programming. Operators allow developers to perform calculations, comparisons, logical evaluations, assignments, and data manipulation. Expressions combine variables, values, and operators to produce results that drive business logic and application behavior.
Whether creating Apex Classes, Triggers, Batch Jobs, Flows with Apex Actions, or Integrations, developers constantly use operators and expressions to make decisions, calculate values, validate records, and automate processes.
Understanding Operators and Expressions is essential for Salesforce Developers because they form the foundation for conditional logic, loops, calculations, and business automation.
An Operator is a symbol that performs a specific operation on one or more values.
Examples:
Operators help manipulate data and control program execution.
An Expression is a combination of:
that produces a result.
Example:
Integer total = 100 + 50;
Expression:
100 + 50
Result:
150
Expressions are used throughout Apex programs.
Operators help developers:
Without operators, applications cannot process information effectively.
Apex supports several categories of operators.
Each serves a different purpose.
Arithmetic Operators perform mathematical calculations.
| Operator | Description |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
These operators are widely used in business applications.
Adds two values.
Example:
Integer totalStudents = 100 + 50;
System.debug(totalStudents);
Output:
150
Addition is commonly used in calculations.
Subtracts one value from another.
Example:
Integer remainingSeats = 100 - 25;
System.debug(remainingSeats);
Output:
75
Useful for inventory and capacity tracking.
Multiplies values.
Example:
Integer totalFee = 5000 * 10;
System.debug(totalFee);
Output:
50000
Commonly used in financial calculations.
Divides one value by another.
Example:
Integer average = 100 / 5;
System.debug(average);
Output:
20
Useful for averages and ratios.
Returns the remainder after division.
Example:
Integer result = 10 % 3;
System.debug(result);
Output:
1
Useful in looping and validation logic.
Stores a value in a variable.
Example:
String courseName = 'Salesforce';
The value is assigned to the variable.
Assignment is one of the most frequently used operations.
Apex supports shortcut assignment operators.
| Operator | Meaning |
|---|---|
| += | Add and Assign |
| -= | Subtract and Assign |
| *= | Multiply and Assign |
| /= | Divide and Assign |
These operators simplify code.
Integer totalStudents = 100;
totalStudents += 20;
System.debug(totalStudents);
Output:
120
Equivalent to:
totalStudents = totalStudents + 20;
Relational Operators compare values.
| Operator | Description |
|---|---|
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal |
| <= | Less Than or Equal |
Relational operators return Boolean values.
Example:
Integer revenue = 50000;
Boolean result = revenue > 10000;
System.debug(result);
Output:
true
Used in business rule validation.
Example:
Integer age = 15;
Boolean result = age < 18;
System.debug(result);
Output:
true
Useful in eligibility checks.
Example:
Integer score = 80;
Boolean result = score >= 75;
Output:
true
Commonly used in grading systems.
Example:
Integer feeBalance = 0;
Boolean result = feeBalance <= 0;
Output:
true
Useful for payment validations.
Equality Operators compare whether values are equal.
| Operator | Description |
|---|---|
| == | Equal To |
| != | Not Equal To |
These operators are frequently used in decision-making.
Example:
String course = 'Salesforce';
Boolean result =
course == 'Salesforce';
Output:
true
Checks whether values match.
Example:
String status = 'Inactive';
Boolean result =
status != 'Active';
Output:
true
Used to exclude conditions.
Logical Operators combine multiple conditions.
| Operator | Description |
|---|---|
| && | AND |
| || | OR |
| ! | NOT |
These operators are essential in business logic.
Returns true only if all conditions are true.
Example:
Integer age = 25;
Boolean feePaid = true;
Boolean result =
(age >= 18) && feePaid;
Output:
true
Both conditions must be satisfied.
Returns true if at least one condition is true.
Example:
Boolean result =
(true || false);
Output:
true
Useful when multiple conditions are acceptable.
Reverses a Boolean value.
Example:
Boolean isActive = true;
System.debug(!isActive);
Output:
false
Used to invert conditions.
Increases a value by one.
Example:
Integer count = 10;
count++;
System.debug(count);
Output:
11
Frequently used in loops.
Decreases a value by one.
Example:
Integer count = 10;
count--;
System.debug(count);
Output:
9
Useful in counting operations.
Provides shorthand decision-making.
Syntax:
condition ? value1 : value2
Example:
Integer marks = 80;
String result =
(marks >= 40)
? 'Pass'
: 'Fail';
Output:
Pass
This reduces code complexity.
Expressions combine operators and values.
Example:
Integer totalFee =
(5000 * 10) + 1000;
Expression:
(5000 * 10) + 1000
Output:
51000
Expressions produce useful business results.
Integer students = 100;
Integer fee = 15000;
Decimal revenue =
students * fee;
Output:
1500000
Used in reporting and calculations.
Apex follows mathematical precedence rules.
Example:
Integer result =
10 + 5 * 2;
Output:
20
Multiplication executes first.
Using parentheses improves readability.
Example:
Integer result =
(10 + 5) * 2;
Output:
30
Parentheses override default precedence.
A software training company calculates enrollment revenue.
Integer students = 200;
Decimal fee = 15000;
Decimal revenue =
students * fee;
Boolean targetAchieved =
revenue >= 2500000;
Output:
Revenue = 3000000
Target Achieved = true
This demonstrates practical operator usage.
Improve readability.
Keep logic simple.
Clarify calculations.
Prevent errors.
Ensure accuracy.
These practices improve code quality.
Developers should review expressions carefully.
Understanding Operators and Expressions helps professionals:
Operators are essential in every Apex program.
Operators perform actions such as calculations, comparisons, assignments, and logical evaluations, while Expressions combine operators, variables, and values to produce results. Apex supports Arithmetic, Assignment, Relational, Equality, Logical, Increment, Decrement, and Conditional Operators. Mastering Operators and Expressions is essential for creating business logic, automating processes, and building scalable Salesforce applications.
An operator is a symbol that performs an action on one or more values.
An expression is a combination of variables, values, and operators that produces a result.
The == operator is used to compare equality.
The && operator is used for logical AND.
The Ternary Operator is a shorthand conditional expression using ? and :.
They allow developers to perform calculations, comparisons, and business logic operations.
Looking to learn more technologies and programming skills?
WhatsApp us