In Salesforce Apex, operators are used to perform various operations on variables, values, and expressions. Apex supports a wide range of operators, including arithmetic operators, comparison operators, logical operators, assignment operators, and more. Here are the commonly used operators in Apex:
- Arithmetic Operators:
- Addition (+): Adds two values.
- Subtraction (-): Subtracts the second value from the first.
- Multiplication (*): Multiplies two values.
- Division (/): Divides the first value by the second.
- Comparison Operators:
- Equal 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 (<=): Checks if the first value is less than or equal to the second.
- Logical Operators:
- AND (&&): Returns true if both conditions are true.
- OR (||): Returns true if at least one condition is true.
- NOT (!): Returns the opposite of the condition.
- Assignment Operators:
- Assign (=): Assigns a value to a variable.
- Increment/Decrement Operators:
- Increment (++) and decrement (–): Increase or decrease the value of a variable by 1.
- String Concatenation Operator:
- Plus (+): Concatenates two strings.
These operators can be used in Apex to perform calculations, make comparisons, control flow, and more. They are fundamental for manipulating data and controlling the logic in your Apex code.
Table of Contents
ToggleOperators in Apex: Arithmetic Operators
Integer a = 5;
Integer b = 3;
Integer sum = a + b; // Addition
Integer difference = a - b; // Subtraction
Integer product = a * b; // Multiplication
Integer quotient = a / b; // Division
System.debug('Sum: ' + sum); // Output: Sum: 8
System.debug('Difference: ' + difference); // Output: Difference: 2
System.debug('Product: ' + product); // Output: Product: 15
System.debug('Quotient: ' + quotient); // Output: Quotient: 1
Operators in Apex: Comparison Operators
Integer a = 5;
Integer b = 3;
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('Is Equal: ' + isEqual); // Output: Is Equal: false
System.debug('Is Not Equal: ' + isNotEqual); // Output: Is Not Equal: true
System.debug('Is Greater: ' + isGreater); // Output: Is Greater: true
System.debug('Is Less: ' + isLess); // Output: Is Less: false
System.debug('Is Greater or Equal: ' + isGreaterOrEqual); // Output: Is Greater or Equal: true
System.debug('Is Less or Equal: ' + isLessOrEqual); // Output: Is Less or Equal: false
Operators in Apex: Logical Operators
Boolean condition1 = true;
Boolean condition2 = false;
Boolean andResult = condition1 && condition2; // AND
Boolean orResult = condition1 || condition2; // OR
Boolean notResult = !condition1; // NOT
System.debug('AND Result: ' + andResult); // Output: AND Result: false
System.debug('OR Result: ' + orResult); // Output: OR Result: true
System.debug('NOT Result: ' + notResult); // Output: NOT Result: false
Safe Navigation Operator in Apex
Apex introduced the Safe Navigation Operator (?.) in Winter ’20 release (API version 48.0). The Safe Navigation Operator provides a concise way to access and perform operations on nested objects or fields without encountering a NullPointerException if any intermediate object or field is null.
Example 4:
Person Class
public class Person {
public String name;
public Address address;
}
Address Class
public class Address {
public String street;
public String city;
public String state;
}
Anonymous Apex
Person person = new Person();
person.name = 'John Doe';
person.address = new Address();
person.address.street = '123 Main St';
person.address.state = 'California';
// Accessing nested fields without Safe Navigation Operator
String fullAddress1;
if (person != null && person.address != null) {
fullAddress1 = person.address.street + ', ' + person.address.city + ', ' + person.address.state;
} else {
fullAddress1 = null;
}
// Accessing nested fields with Safe Navigation Operator
String fullAddress2 = person?.address?.street?.toUpperCase() + ' '+ person?.address?.city?.toUpperCase()+', ' + person?.address?.state?.toUpperCase();
System.debug('Full Address 1: ' + fullAddress1);
System.debug('Full Address 2: ' + fullAddress2);
let’s do another example using Safe Navigation operator.
AccountExample
public class AccountExample {
public static void getAccountDescriptionWithoutSafeNavigation(String accountNumber){
List<Account> allAccounts = [SELECT
Id,
Description
FROM Account
WHERE AccountNumber = :accountNumber ];
if(!allAccounts.isEmpty() && allAccounts.get(0).Description != null)
{
String accountDescription = allAccounts.get(0).Description.toUpperCase();
System.Debug('Account Description: ' + accountDescription);
}
}
public static void getAccountDescriptionWithSafeNavigation(String accountNumber){
String accountDescription;
accountDescription = [SELECT Id,
Description
FROM Account
WHERE AccountNumber=:accountNumber]?.Description?.toUpperCase();
System.debug('Account Description: ' + accountDescription);
}
}
Anonymous Apex
AccountExample.getAccountDescriptionWithoutSafeNavigation('CD451796');
AccountExample.getAccountDescriptionWithSafeNavigation('CD451796');
Enroll in our Complete Salesforce Developer Training for Beginners