There’s a new operator in town! Sometimes we want to know if two values are not equal to each other.
Apex used the !=
operator to detect if two Integers are not equal. The operator compares the value on the left-hand side with the value on the right-hand side. If they’re the same value, it will return false
. If they are different values, it will return true
.
This can be a little tricky to wrap your head around at first. But with practice comes mastery!
Here’s an example where the not equals operator is used on two Integer values that are the same:
Integer actualTcv = 4500; Integer expectedTCV = 4500; Boolean tcvMatch = actualTcv != expectedTCV; // evaluates to False, because the two values are equal
In this case, the statement is asking “Is 4500 not equal to 4500?”. The answer is No, they are equal, they reference the same value. So the result is False.
Here’s an example of the outcome when the not equals operator is used on two Integers that have different values:
Integer tcv2 = 4500; Integer expectedTCV2 = 3214; Boolean tcvMatch2 = tcv2 != expectedTCV2; // evaluates to True because they are not equal.
In this case, since the left-hand and right-hand values are different, the !=
operator returns true. The statement is asking “Is 4500 not equal to 3214?”. The answer is Yes, they’re not equal. So the result is true.