Experience Salesforce
Equality Operator
What You’ll Learn
- What Is Equality Operator In Apex?
- What Is Exact Equality Operator(===)?
- What Is Exact Inequality Operator(!==)?
Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
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.
Next TopicNeed Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.