The snippet you posted in the comments is failing due to a common mistake. Its an order of operations error, because you're adding an extra string to your comparison, it gets merged into your string value, and screws up your comparison.
Your code is evaluated like this:
'==>' + 'ab' == 'ab'
('==>' + 'ab') == 'ab'
'==>ab' == 'ab' // false
If you leave out the '==>'
, you get this comparison instead:
'ab' == 'ab' // true
You can get it to output as expected by wrapping your comparison in ()
to seperate it from your outputted text.
System.debug('==>' + (ab == bc)); // '==>true'
This happens due to the Operator Precedence of +
being higher than ==
. Another way to phrase this would be that an +
operation is evaluated before ==
. Since ()
are evaluated first, we can force the order of operations to happen a certain way by wrapping the right code.
==
will get you exact comparison for a large number of use cases. I've written an example which creates a pair type, Tuple
, creates a number of test cases, and then tests all of them. All of these equality checks are successful.
==
compares by value, expect for a few cases, such as user defined objects. If you expect a object which you wrote yourself to be checked using ==
, you should provide an implementation for equals
, otherwise those objects will be checked by reference.
A == B
is equal to A.equals(B)
, for all intents and purposes. This means that no matter which types they are, if the equals
function has been defined for that type (which it will be for all standard objects and sObjects), you'll get an accurate comparison.
Checking by value means that the exact field values will be compared, or the value of the object itself. Checking by reference means that the memory locations of these objects will be compared. You can force checking by reference by using ===
instead.
==
is well documented in Expresssion Operators
See this example:
public class Tuple {
public Object A { get; set; }
public Object B { get; set; }
public Tuple(Object A, Object B) {
this.A = A;
this.B = B;
}
}
List<Tuple> tuples = new List<Tuple>();
tuples.add(new Tuple('a', 'a'));
tuples.add(new Tuple(1, 1));
tuples.add(new Tuple(1.7, 1.7));
tuples.add(new Tuple(true, true));
tuples.add(new Tuple(Date.newInstance(1990, 1, 1), Date.newInstance(1990, 1, 1)));
tuples.add(new Tuple(DateTime.newInstance(1990, 1, 1, 0, 0, 0), DateTime.newInstance(1990, 1, 1, 0, 0, 0)));
tuples.add(new Tuple('006c000000Gzm9f', '006c000000Gzm9f'));
tuples.add(new Tuple(600L, 600L));
tuples.add(new Tuple(new Account(Name='Test'), new Account(Name='Test')));
for (Tuple t:tuples) {
System.assert(t.A == t.B);
}
Object ab = 'ab'; Object bc = 'ab'; system.debug('==>' + ab==bc);