Sub topic 15
Comparison and equality
This chapter assumes you know how to declare Apex variables, what null is, and how Strings and collections are typed. Comparison operators return a Boolean, and several of them behave differently in Apex than in Java.
The operators
| Operator | Meaning |
|---|---|
| `==` | Equality of value (for strings, ignoring case) |
| `!=` | Inequality |
| `===` | Exact equality: same object in memory |
| `!==` | Exact inequality |
| `<`, `<=`, `>`, `>=` | Ordering |
Strings ignore case with `==`
In Apex, == on Strings is case-insensitive. Use equals when case matters.
String a = 'apex';
String b = 'APEX';
System.debug(a == b);
System.debug(a.equals(b));
System.debug(a.equalsIgnoreCase(b));This prints true, false, true. The ordering operators on Strings are also case-insensitive. Because == is already case-insensitive, equalsIgnoreCase is mostly there to make your intent clear.
Null in comparisons
== and != work with null and never throw an error. Two nulls are equal.
String note;
Integer total = 5;
System.debug(note == null);
System.debug(total != null);
System.debug(null == null);Calling a method on a null value does throw a NullPointerException. Write note.equals('x') only after you know note is not null, or write 'x'.equals(note) instead.
Collections and SObjects compare by content
For Lists, == is true when both lists have the same size and equal elements in the same order. Sets compare their members, and Maps compare their key-value pairs. SObjects compare field values.
List<Integer> first = new List<Integer>{ 1, 2, 3 };
List<Integer> second = new List<Integer>{ 1, 2, 3 };
System.debug(first == second);
System.debug(first === second);
Account x = new Account(Name = 'Acme');
Account y = new Account(Name = 'Acme');
System.debug(x == y);
System.debug(x === y);The output is true, false, true, false. The == comparisons check content. The === comparisons check whether both variables point to the same object, and these are two separate objects.
Ids and numbers
Two Ids that identify the same record are equal even if one is the 15-character form and the other is the 18-character form. When you compare numeric types, Apex widens them first. Integer 3 == Decimal 3.0 is true.
Integer i = 3;
Decimal d = 3.0;
System.debug(i == d);
System.debug(0.1 + 0.2 == 0.3);
System.debug(Decimal.valueOf('0.1') + Decimal.valueOf('0.2') == Decimal.valueOf('0.3'));The second line is false because 0.1 + 0.2 is evaluated as a Double, which has binary rounding error. The third line is true because Decimal stores exact base-10 values.
Common errors
- Assuming
'Abc' == 'abc'is false, as it would be in Java. - Using
===on two Lists and expecting content comparison. - Comparing a possibly null String with
.equalsand getting aNullPointerException. - Comparing Booleans or Dates with
<without checking for null first. A null operand in an ordering comparison makes the resultfalse, not an error.
Exercise
Given String a = 'Hello'; String b = 'hello';, what do a == b, a.equals(b), and a === b return, and why might a === b differ from a == b in general?
Show answer
a == b is true because Apex string == ignores case. a.equals(b) is false because equals is case-sensitive. a === b is not true for these two values, because it requires exact equality, and they differ in case. In general, === checks identity for objects, so two separate objects with equal contents are == but not ===.