Apex Operators
Operators are special symbols used to perform operations on variables and values. These operations can be mathematical, logical, comparative, or assignment based. In Apex, just like in other programming languages, operators help us manipulate data, make decisions, and control program flow.
Let’s break them down into different categories:
1. Arithmetic Operators
Used to perform basic math operations.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 10 + 5 | 15 |
– | Subtraction | 10 – 3 | 7 |
* | Multiplication | 4 * 3 | 12 |
/ | Division | 10 / 2 | 5 |
% | Modulus (remainder) | 7 % 4 | 3 |
2. Relational (Comparison) Operators
Used to compare two values and return true or false
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 4 != 3 | true |
> | Greater than | 7 > 5 | true |
< | Less than | 2 < 4 | true |
<= | Less than or equal to | 3 <= 2 | False |
>= | Greater than or equal to | 6 >= 6 | true |
3. Logical Operators
Used for combining or inverting Boolean expressions (true/false).
Operator | Description | Example | Output |
---|---|---|---|
&& | Logical AND | true && false | false |
! | Logical NOT | false | false |
Note:
AND (&&): Returns true if both sides are true
OR (||): Returns true if any one side is true
NOT (!): Reverses the Boolean value
4. Assignment Operators
Used to assign values to variables.
Operator | Description | Example | Output |
---|---|---|---|
= | Assign | a = 5 | Assign 5 to a |
+= | Add and assign | a += 2 | a = a + 2 |
-= | Subtract and assign | a -= 3 | a = a – 3 |
*= | Multiply and assign | a *= 2 | a = a * 2 |
/= | Divide and assign | a /= 2 | a = a / 2 |
%= | Modulus and assign | a %= 2 | a = a % 2 |
Code Example:
1. Arithmetic + Assignment
Integer a = 10;
a += 5; // a = a + 5
System.debug(‘Result: ‘ + a);
Code Explanation:
• Integer a = 10;: Initializes variable a with the value 10.
• a += 5;: Adds 5 to the current value of a. (Equivalent to a = a + 5, so a becomes 15)
• System.debug(‘Result: ‘ + a);: Prints the final value of a with
Output:
Result:15
2. Logical and Relational
Integer age = 20;
Boolean isAdult = age >= 18 && age < 60;
System.debug(‘Is Adult: ‘ + isAdult);
Code Explanation:
• Integer age = 20;: Initializes a variable age with value 20.
• Boolean isAdult = age >= 18 && age < 60;:
• Checks two conditions using the logical AND operator &&:
• age >= 18: Is age greater than or equal to 18? (Yes, 20 ≥ 18) • age < 60: Is age less than 60? (Yes, 20 < 60)
• Since both conditions are true, isAdult is set to true.
• System.debug(‘Is Adult: ‘ + isAdult);: Prints the result of the logical check.
Output:
Is Adult:true
3. Using Modulus and Comparison
Integer number = 9;
Boolean isEven = (number % 2 == 0);
System.debug(‘Is the number even? ‘ + isEven);
Code Explanation:
• Integer number = 9;: Initializes a variable number with value 9.
• number % 2 == 0:
• % is the modulus operator, which gives the remainder after division.
• 9 % 2 gives 1 (since 9 divided by 2 leaves a remainder of 1).
• So, (number % 2 == 0) evaluates to false.
• Boolean isEven = (number % 2 == 0);: Stores the result of the check in the Boolean variable isEven.
• System.debug(‘Is the number even? ‘ + isEven);: Prints whether the number is even or not.
Output:
Is the number even? false
4. Logical OR and NOT
Boolean isRainy = false;
Boolean isSnowy = true;
Boolean stayHome = isRainy || isSnowy;
System.debug(‘Should I stay home? ‘ + stayHome);
Boolean goOutside = !stayHome;
System.debug(‘Can I go outside? ‘ + goOutside);
Code Explanation:
• Boolean isRainy = false;: Sets isRainy to false.
• Boolean isSnowy = true;: Sets isSnowy to true.
• Boolean stayHome = isRainy
|| isSnowy;:
|| is the logical OR operator.
If either isRainy or isSnowy is true, then stayHome becomes true.
Since isSnowy is true, stayHome is true.
• System.debug(‘Should I stay home? ‘ + stayHome);: Prints the value of stayHome.
• Boolean goOutside = !stayHome;:
! is the logical NOT operator.
It reverses the value of stayHome.
Since stayHome is true, goOutside becomes false.
• System.debug(‘Can I go outside? ‘ + goOutside);: Prints whether you can go outside.
Output:
Should I stay home? true
Can I go outside? false
Tasks for Practice
Task 1: Write an Apex program to calculate the sum, difference, product, quotient, and remainder of two numbers using arithmetic operators.
Task 2: Write an Apex program that compares two numbers and prints:
• if they are equal,
• which one is greater.
Task 3: Write an Apex program that checks if a person is eligible to vote using relational and logical operators (age >= 18 AND isCitizen == true).
Task 4: Write an Apex program using assignment operators to:
• Start with a value a = 5
• Add 3 to it
• Multiply the result by 2
• Subtract 4
Print the final result after each step.
Task 5: Write an Apex program that takes two numbers, 25 and 4, and prints their:
• Sum
• Difference
• Product
• Quotient
• Remainder
Task 6: Create a boolean logic checker:
• Define three boolean variables: isStudent = true, hasID = true, hasPaid = false
• Use logical operators (&&, ||, !) to determine if the person is eligible for the exam.
• Print the result.
Task 7: Write a program that compares two integers:
Integer a = 15;
Integer b = 20;
Use comparison operators (>, <, >=, <=, ==, !=) to check various conditions and print the results.