SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Monday, 7 October 2024

Expressions and Operators in Apex - Salesforce Apex Tutorial Part 17 to Part 24

Aloha Trailblazers,


In this post, I'm sharing the code snippet for Coding with Confidence: The Fun Way to Learn Salesforce Apex tutorial series part 17 and above:
// * Expressions and Operators in Apex

/*
? What is an Expression?
* An expression is a combination of variables, operators, and method calls that results in a specific value. There can be different types of expressions:
*/

// * Literal expression

Integer num = 2+3;
System.debug('2+3=' + num);

/*
*   New instance of an object, list or map
*   Left hand of an assignment operators
*/

List<Integer> numbers = new List<Integer>();
Map<Integer, String> studentsMap = new Map<Integer, String>();

// * Other expressions include SOQL, static method invocation etc.

/*
? What are Expression Operators?
* Expression operators can join expressions to create compound expressions. For example:
*/

Integer num = (2+3) + (5-4);
System.debug(num);

// * Different types of operators in apex:

Integer num = 1;
System.debug('Value of num = ' + num);

// * Assignment operator
num = 10;
System.debug('Value of num after assigning 10 = ' + num);

// * Addition operator
Integer num;
num = 5 + 10;
System.debug('Value of num (5+10) = ' + num);

// * Subtraction operator
num = 5 - 10;
System.debug('Value of num (10-5) = ' + num);

// * Multiplication operator
Integer num;
num = 10 * 5;
System.debug('Value of num (10*5) = ' + num);

// * Division operator
Decimal num;
num = 10.0 / 3.0;
System.debug('Value of num (10/5) = ' + num);

Integer num = 0;
System.debug('Value of num, Prefix = ' + ++num + ', Postfix = ' + num++ + ', Postfix = ' + num++);
System.debug('Value of num = ' + num);

// * Increment operator
num++;
System.debug('Value of num after increment = ' + num);

// * Decrement operator
num--;
System.debug('Value of num after decrement = ' + num);

// * Prefix and Postfix concept

// * Part 2

// * Negation operator
Integer num = -10;
System.debug('Value of negative num = ' + -num);

Integer num = 5;
// * Addition Assignment operator
num += 15;
// num = num + 15;
System.debug('Value of num = ' + num);

Integer num = 5;
// * Multiplication Assignment operator
num *= 2; // * num = num * 2;
System.debug('Value of num = ' + num);

Integer num = 5;
// * Subtraction Assignment operator
num -= 5; // * num = num - 5;
System.debug('Value of num = ' + num);

Integer num = 10;
// * Division Assignment operator
num /= 5; // * num = num / 5;
System.debug('Value of num = ' + num);

Boolean a = false, b = false, c;

// * AND operator
c = a && b;
System.debug('a && b = ' + c);

Boolean a = true, b = true, c;
// * OR operator
c = a || b;
System.debug('a || b = ' + c);

Boolean a = true, b = false, c;
// * NOT operator - Unary operator
c = !a;
System.debug('!a = ' + c);
c = !b;
System.debug('!b = ' + c);

// * A note about Null pointer exception. This exception occurs when you're trying to use a variable and the value of that variable is null.

Boolean a = true, b, c = false;
a = c || b;
System.debug('Result = ' + a);

// * Less than operator
Boolean c = 25 < 25;
System.debug('Is 5 less than 10? -> ' + c);

// * Greater than operator
Boolean c = 10 > 25;
System.debug('Is 10 greater than 5? -> ' + c);

// * Less than or equal to operator
Boolean c = 25 <= 25;
System.debug('Is 5 less than or equal to 10? -> ' + c);

// * Greater than or equal to operator
Boolean c = 10 >= 20;
System.debug('Is 10 greater than or equal to 5? -> ' + c);


// * Equality operator - this checks if the values are equal
Integer num1 = 5, num2 = 5;
Boolean result = num1==num2;
System.debug('Are num1 and num2 equal? -> ' + result);

Integer num1 = 5, num2 = 10;
Boolean result = num1==num2;
System.debug('Are num1 and num2 equal? -> ' + result);

// * Inequality operator
Integer num1 = 5, num2 = 5;
Boolean result = num1!=num2;
System.debug('Are num1 and num2 not equal? -> ' + result);

// * Exact equality operator
Integer num1 = 5, num2 = 5;
Boolean result = num1===num2;
System.debug('Are num1 and num2 equal? -> ' + result);

List<String> words1 = new List<String>{'Richard', 'Hendricks'};
List<String> words2 = words1;
Boolean result = words1===words2;
System.debug('words1 and words2 are equal? -> ' + result);

// words2 = new List<String>{'Richard', 'Hendricks'};
// result = words1===words2;
// System.debug('words1 and words2 are equal? -> ' + result);

// * Exact Inequality operator
result = words1!==words2;
System.debug('words1 and words2 are not equal? -> ' + result);

// * Paranthesis - Operator Precedence
Integer result = 22+2-4/2*5;
System.debug('Value of result = ' + result);

Integer result = (22+2-4)/(2*5);
System.debug('Value of result = ' + result);

// * Ternary Operator
Integer age = 15;
String result = age >= 18 ? 'Richard can drive' : 'Richard cannot drive';
System.debug(result);

/*
* Safe navigation operator - It is represented by ?.
* This operator follows the rule, if the left hand side of chain is null, the right hand side isn't evaluated and null is returned. Otherwise, right hand side is evaluated and it's value is returned. This saves the code from throwing a NullPointerException
*/
List<Integer> numbers = new List<Integer>{1, 2, 3, 4, 5};
Integer size = numbers?.size();
System.debug('List size = ' + size);

// * Note: Safe navigation operator cannot be used at the left side of an assignment

Integer a = 10, b = null;
a?.b = 5;

/*
* Null Coalescing Operator - It is represented by ??
* This operator returns the left hand argument if the left hand argument isn't null. Otherwise it returns the right hand argument. This operator is left-associative, it means that only the left-hand operand is evaluated first. The right-hand operand is evaluated ONLY if the left-hand operand is null
*/

Integer num = 20;
Integer notNullNum = num ?? 10;
System.debug('Number = ' + num);
System.debug('Not null number = ' + notNullNum);

// * Note: Null coalescing operator cannot be used at the left side of an assignment
Integer num;
Integer num1 = 10;
// num??num1++; // * a++ => a = a+1; num??num1++ => num??num1 = num??num1 + 1;
num??num1 = 11;

// * Operator Precedence: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_precedence.htm

YouTube Tutorials

You can check out the tutorials below as well:

Expressions and Expression Operators in Apex - Salesforce Apex Tutorial Part 17

Arithmetic Operators in Apex - Salesforce Apex Tutorial Part 18

Increment and Decrement Operators, Prefix and Postfix in Apex - Salesforce Apex Tutorial Part 19

Negation and Arithmetic Assignment Operators in Apex - Salesforce Apex Tutorial Part 20

Boolean Operators in Apex, Truth Table and Null Pointer Exception - Salesforce Apex Tutorial Part 21

Greater/Less Than, Greater/Less Than Equal To Boolean Operators - Salesforce Apex Tutorial Part 22

Equality, Inequality, Exact Equality & Exact Inequality Operators - Salesforce Apex Tutorial Part 23

Operators: Precedence, Ternary, Safe Navigation & Null Coalescing - Salesforce Apex Tutorial Part 24


Check out the tutorials and let me know your feedback in the comments down below.

Happy Trailblazing!!

No comments:

Post a Comment