0% found this document useful (0 votes)
15 views

Operators In Apex

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Operators In Apex

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Operators In Apex

In Apex, operators are symbols used to perform operations on variables and values.
Operators in Apex can be categorized into various types, such as arithmetic,
assignment, comparison, logical, and bitwise operators. Below is a comprehensive
guide on these operators with examples.

1. Arithmetic Operators

These operators perform basic mathematical operations on numerical values.

Operat
Description Example
or
+ Addition 5 + 3 => 8
- Subtraction 5 - 3 => 2
* Multiplication 5 * 3 => 15
/ Division 5 / 3 => 1.666...
Modulus
% 5 % 3 => 2
(remainder)

Example:

Integer a = 10;
Integer b = 3;
Integer sum = a + b; // 13
Integer difference = a - b; // 7
Integer product = a * b; // 30
Double division = a / (Double)b; // 3.3333
Integer remainder = a % b; // 1

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exam
Description
or ple
= Assigns a value x=5
+= Adds and assigns x += 5
Subtracts and
-= x -= 5
assigns
Multiplies and
*= x *= 5
assigns
/= Divides and assigns x /= 5
%= Modulus and assigns x %= 5
Example:

Integer x = 10;
x += 5; // x = x + 5, result is 15
x -= 3; // x = x - 3, result is 12
x *= 2; // x = x * 2, result is 24
x /= 4; // x = x / 4, result is 6
x %= 5; // x = x % 5, result is 1

3. Comparison Operators

These operators compare two values and return a Boolean result (true or false).

Operat
Description Example
or
5 == 5 =>
== Equal to
true
!= Not equal to 5 != 3 => true
> Greater than 5 > 3 => true
< Less than 5 < 3 => false
Greater than or equal 5 >= 5 =>
>=
to true
5 <= 3 =>
<= Less than or equal to
false

Example:

Integer a = 10;
Integer b = 5;
Boolean result1 = a > b; // true
Boolean result2 = a <= b; // false
Boolean result3 = a == b; // false
Boolean result4 = a != b; // true

4. Logical Operators

Logical operators are used to combine multiple Boolean expressions.

Operat Descripti
Example
or on
Logical true && false =>
&&
AND false
|| Logical OR true || false → true
Logical
! !true => false
NOT
Example:

Boolean a = true;
Boolean b = false;
Boolean result1 = a && b; // false
Boolean result2 = a || b; // true
Boolean result3 = !a; // false

5. Bitwise Operators

Bitwise operators perform bitwise operations on integer values.

Operat Descripti
Example
or on
Bitwise
& 5 & 3 => 1
AND
` ` Bitwise OR
Bitwise
^ 5 ^ 3 => 6
XOR
Bitwise
~ ~5 => -6
NOT
5 << 1 =>
<< Left shift
10
5 >> 1 =>
>> Right shift
2

Example:

Integer a = 5; // 0101 in binary


Integer b = 3; // 0011 in binary
Integer andResult = a & b; // 1 (0001 in binary)
Integer orResult = a | b; // 7 (0111 in binary)
Integer xorResult = a ^ b; // 6 (0110 in binary)
Integer notResult = ~a; // -6 in two's complement
Integer leftShift = a << 1; // 10
Integer rightShift = a >> 1; // 2

6. Ternary Operator

The ternary operator (? :) is a shorthand for if-else statements. It evaluates a


condition and returns one of two values based on whether the condition is true or
false.
condition ? value_if_true : value_if_false;

Example:

Integer age = 18;


String result = (age >= 18) ? 'Adult' : 'Minor'; // Outputs 'Adult'

7. Instanceof Operator

The instanceof operator checks if an object is an instance of a specific class or


interface.

Account acc = new Account();


Boolean isAccount = acc instanceof Account; // true
Boolean isString = acc instanceof String; // false

8. Special Operators in Apex

new: Creates a new instance of an object.

Account acc = new Account();

instanceof: Checks the type of an object at runtime.

Boolean check = myObj instanceof Account;

with sharing / without sharing: Controls record access permissions in


classes.

public with sharing class MyClass {}

this: Refers to the current instance of a class.

public class MyClass {


public void display() {
System.debug(this); // Refers to the current instance
}
}

Summary of Key Points


 Arithmetic Operators perform basic math operations.
 Assignment Operators set and update variable values.
 Comparison Operators evaluate relationships between values, returning Boolean results.
 Logical Operators combine Boolean expressions.
 Bitwise Operators perform operations at the binary level.
 Ternary Operator provides an inline if-else.
 Instanceof Operator checks object type at runtime.
Questions

1. Which of the following operators in Apex is used for


string concatenation?
 A) +
 B) -
 C) *
 D) =

(Answer: A) +

2. What is the output of the following Apex code


snippet?

Integer x = 10;
Integer y = 3;
Integer result = x % y;
System.debug(result);

 A) 0
 B) 1
 C) 2
 D) 3

(Answer: B) 1

3. In Apex, which operator checks if two variables


refer to the same object in memory?
 A) ==
 B) =
 C) !=
 D) instanceof

(Answer: A) ==
4. What is the purpose of the || operator in Apex?
 A) It performs an AND operation between two Boolean expressions.
 B) It performs an OR operation between two Boolean expressions.
 C) It assigns a value to a variable.
 D) It checks if two values are equal.

(Answer: B) It performs an OR operation between two Boolean expressions.

5. Which of the following correctly describes the +=


operator in Apex?
 A) It increments the variable by 2.
 B) It divides the variable by another value and assigns the result.
 C) It adds a value to the variable and assigns the result to that variable.
 D) It subtracts a value from the variable and assigns the result to that variable.

(Answer: C) It adds a value to the variable and assigns the result to that variable.

You might also like