Operators and Expressions
Operators and Expressions in Salesforce Apex
In Salesforce Apex, operators and expressions are fundamental building blocks that allow you to perform various operations on data. Understanding how they work is crucial for writing effective and efficient code. Let’s break down operators and expressions and see how they work with an original example.
1. Operators in Apex
Operators are special symbols or keywords that perform operations on one or more operands (variables or values) and return a result. Apex supports several types of operators:
1.1 Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus.
- Addition (
+
): Adds two operands. - Subtraction (
-
): Subtracts the second operand from the first. - Multiplication (
*
): Multiplies two operands. - Division (
/
): Divides the first operand by the second. - Modulus (
%
): Returns the remainder when the first operand is divided by the second.
Example:
Integer num1 = 10;
Integer num2 = 3;
Integer sum = num1 + num2; // sum = 13
Integer difference = num1 - num2; // difference = 7
Integer product = num1 * num2; // product = 30
Integer quotient = num1 / num2; // quotient = 3
Integer remainder = num1 % num2; // remainder = 1
1.2 Comparison Operators
Comparison operators are used to compare two values. These operators return a Boolean value (true
or false
).
- Equal to (
==
): Checks if two operands are equal. - Not equal to (
!=
): Checks if two operands are not equal. - Greater than (
>
): Checks if the first operand is greater than the second. - Less than (
<
): Checks if the first operand is less than the second. - Greater than or equal to (
>=
): Checks if the first operand is greater than or equal to the second. - Less than or equal to (
<=
): Checks if the first operand is less than or equal to the second.
Example:
Integer age = 25;
Boolean isAdult = age >= 18; // isAdult = true
Boolean isSenior = age > 65; // isSenior = false
1.3 Logical Operators
Logical operators are used to combine multiple Boolean expressions or values and return a Boolean result.
- AND (
&&
): Returns true if both operands are true. - OR (
||
): Returns true if at least one operand is true. - NOT (
!
): Returns true if the operand is false.
Example:
Boolean hasLicense = true;
Boolean isAbove18 = true;
Boolean canDrive = hasLicense && isAbove18; // canDrive = true
Boolean isWeekend = false;
Boolean canGoForTrip = canDrive || isWeekend; // canGoForTrip = true
1.4 Assignment Operators
Assignment operators are used to assign values to variables.
- Simple Assignment (
=
): Assigns the value on the right to the variable on the left. - Add and Assign (
+=
): Adds the right operand to the left operand and assigns the result to the left operand. - Subtract and Assign (
-=
): Subtracts the right operand from the left operand and assigns the result to the left operand. - Multiply and Assign (
*=
): Multiplies the left operand by the right operand and assigns the result to the left operand. - Divide and Assign (
/=
): Divides the left operand by the right operand and assigns the result to the left operand. - Modulus and Assign (
%=
): Takes the modulus of the left operand by the right operand and assigns the result to the left operand.
Example:
Integer score = 50;
score += 10; // score = 60
score -= 5; // score = 55
score *= 2; // score = 110
score /= 2; // score = 55
score %= 10; // score = 5
1.5 Ternary Operator
The ternary operator is a shorthand for the if-else
statement. It takes three operands and returns a value based on a condition.
Syntax:
condition ? valueIfTrue : valueIfFalse
Example:
Integer age = 20;
String category = (age >= 18) ? 'Adult' : 'Minor'; // category = 'Adult'
2. Expressions in Apex
An expression is a combination of variables, literals, operators, and method invocations that are evaluated to produce a value. Expressions are used to perform calculations, assign values, and make decisions.
Example of Expressions in Apex:
Let’s say you are building a simple system for a movie theater that gives discounts to seniors (age 60 and above) and children (age 12 and below).
Integer customerAge = 65;
Boolean isSenior = customerAge >= 60; // Expression using a comparison operator
Boolean isChild = customerAge <= 12; // Another expression using a comparison operator
// Using expressions to determine discount eligibility
Boolean getsDiscount = isSenior || isChild;
// Example of a more complex expression
Decimal ticketPrice = 10.00;
Decimal discount = getsDiscount ? 2.00 : 0.00; // Ternary operator
Decimal finalPrice = ticketPrice - discount; // Arithmetic operation
Explanation:
- The first two expressions check if the customer qualifies as a senior or a child.
- The
getsDiscount
expression uses a logical operator to determine if the customer is eligible for a discount. - The
discount
expression uses a ternary operator to apply a discount if eligible. - Finally, the
finalPrice
expression calculates the final price of the ticket after applying the discount.