function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Girish Reddy 52Girish Reddy 52 

What is the difference between == , .equals, and compareTo operators in Apex

Can you please give an example for each it would be really helpful.
Thank You
PriyaPriya (Salesforce Developers) 

Hi Girish,

> equals(stringOrId) Returns true if the passed-in object is not null and represents the same binary sequence of characters as the current string. Use this method to compare a string to an object that represents a string or an ID.
== is same as equalsIgnoreCase(secondString)
Returns true if the secondString is not null and represents the same sequence of characters as the String that called the method, ignoring case.

 

Check below example that matches your requirement :- 

https://salesforce.stackexchange.com/questions/80456/is-there-any-difference-in-equals-and-for-string-variables


Comparison Operator :- 
https://www.sfdc99.com/2013/10/12/comparison-operators/

Please mark it as the Best Answer so that it can help others in the future.

Regards,

Priya Ranjan

 

Ashish Singh SFDCAshish Singh SFDC
Hi 

The only point to notice here is if your String doesn't have any value then by default it will be null and hence if you do the below thing it will be a null pointer exception:
 
String str; //No value assigned to str
Object obj1= 'abc'; //Variable which needs to be compared

Boolean result1 = str.equals(obj1); //NULL Pointer Exception

But if you do the same thing with ==(comparator operator), then
String str; //No value assigned to str
Object obj1= 'abc';

Boolean result2 = str==obj1; //Comparision using == operator
System.debug(result2);//False
 
It will depend on your use case when to use what but normally if you go with equals(input) then be mindful to null check equals(input) is called.

Thanks,
Ashish Singh.
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

1.

equals(secondString)
Deprecated. This method is replaced by equals(stringOrId). Returns true if the passed-in string is not null and represents the same binary sequence of characters as the current string. Use this method to perform case-sensitive comparisons.

Example : 

String myString1 = 'abcde';
String myString2 = 'abcd';
Boolean result = myString1.equals(myString2);
System.assertEquals(result, false);

2.

compareTo(secondString)
Compares two strings lexicographically, based on the Unicode value of each character in the Strings.
Signature
public Integer compareTo(String secondString)

 

Usage
The result is:
A negative Integer if the String that called the method lexicographically precedes secondString
A positive Integer if the String that called the method lexicographically follows compsecondStringString
Zero if the Strings are equal
If there is no index position at which the Strings differ, then the shorter String lexicographically precedes the longer String.
Note that this method returns 0 whenever the equals method returns true.

String myString1 = 'abcde';
String myString2 = 'abcd';
Integer result = 
   myString1.compareTo(myString2);
System.assertEquals(result, 1);

3.==  return true or false

String myString1 = 'abcd';
String myString2 = 'abcd';

if(myString1 == myString2 ){
system.debug('Hi');
}
Please mark it as the Best Answer if your queries are solved.

Thank You