3

Equality Operator

What Is Equality Operator In Apex?

If a value of X equals Y, the expression evaluates to true otherwise the expression evaluates to false.

  • User-defined types are compared by reference which means that the 2 objects are equal if they reference the same location in the memory.
  • You can override this default comparison behaviour by equals() and hashcode() methods in your class to compare object values instead.
Note:
  • Unlike java(==), in apex it compares object value equality not reference equality except for user-defined types..
  • String comparison using(==) is case insensitive.
  • ID comparison using == is case-sensitive.

main() 
{ 
Integer a[] = new Integer[5]; 
Integer b[] = new Integer[5]; 
for(Integer i=0; i<5; i++) 
a[i]=i+1; 
System.debug(a); 
System.debug(b); 
System.debug(a==b); 
}

Note:
  • Arrays(==) performs a deep check of all the values before returning its result. Likewise for collection and built-in apex objects.
  • The comparison of any two values can never result in null though X and Y can be literal null.
  • SOQL and SOSL use ‘=’ for their equality operator not ‘==’.

What Is Exact Equality Operator(===)?

If X and Y reference the exact same location in memory, the expression evaluates to true otherwise false.

main()
{
FirstClass fc = new FirstClass();
FirstClass fc2 = new FirstClass();
System.debug(if(fc===fc2));
}

What Is Exact Inequality Operator(!==)?

If X and Y do not reference the exact same location in memory, the expression evaluates to true otherwise false.

salesforce-developer
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

ServiceNow Stripe