Aloha Trailblazers,
// * 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
System.debug('Value of negative num = ' + -num);
// * Addition Assignment operator
num += 5;
System.debug('Value of num = ' + num);
// * Multiplication Assignment operator
num *= 2;
System.debug('Value of num = ' + num);
// * Subtraction Assignment operator
num -= 5;
System.debug('Value of num = ' + num);
// * Subtraction Assignment operator
num /= 5;
System.debug('Value of num = ' + num);
Boolean a = true, b = false, c;
// * AND operator
c = a&&b;
System.debug('a&&b = ' + c);
// * OR operator
c = a||b;
System.debug('a||b = ' + c);
// * NOT operator
c = !a;
System.debug('!a = ' + c);
c = !b;
System.debug('!b = ' + c);
// * Less than operator
Boolean c = 5 < 10;
System.debug('Is 5 less than 10? -> ' + c);
// * Greater than operator
Boolean c = 10 > 5;
System.debug('Is 10 greater than 5? -> ' + c);
// * Less than or equal to operator
Boolean c = 5 <= 10;
System.debug('Is 5 less than 10? -> ' + c);
// * Greater than or equal to operator
Boolean c = 10 >= 5;
System.debug('Is 10 greater than 5? -> ' + c);
// * Equality operator
Integer num1 = 5, num2 = 5;
Boolean result = num1==num2;
System.debug('Are num1 and num2 equal? -> ' + result);
num2 = 10;
result = num1==num2;
System.debug('Are num1 and num2 equal? -> ' + result);
// * Inequality operator
result = num1!=num2;
System.debug('Are num1 and num2 not equal? -> ' + result);
// * Exact equality operator
List<String> words1 = new List<String>{'Richard', 'Hendricks'};
List<String> words2 = words1;
result = words1===words2;
System.debug('words1 and words2 are equal? -> ' + result);
// * Exact Inequality operator
words2 = new List<String>{'Richard', 'Hendricks'};
result = words1!==words2;
System.debug('words1 and words2 are not equal? -> ' + result);
// * Paranthesis
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 = 20;
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;
Integer size = numbers.size();
System.debug('List size = ' + size);
// * Note: Safe navigation operator cannot be used at the left side of an assignment
/*
* 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;
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++;
// num??num1 = 11;
// * Operator Precedence: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_precedence.htm