Sandeep is a ITA at Tata Consultancy Services. He also is the author of java blog. Sandeep has posted 24 posts at DZone. You can read more from them at their website. View Full User Profile

How String equals Method Works

  • submit to reddit

The equals method is declared in Object class and hence is inherited by all classes in Java. The purpose of equals method is to provide logical equality. The default implementation of equals method is:

public boolean equals(Object obj) {
return (this == obj);
Thus the default behavior of equals method is same as == operator.

equals method in String class checks if the two strings have same characters or not. But this check is performed intelligently. The algorithm for checking the Strings for equality involves:

a) If the String objects are equals as per == operator, true is returned.
b) If the == returns false then each character of both the strings are compared and if a difference if found, immediately false is returned.
c) If after comparing all characters of both the strings, no difference is found in characters and length of strings, true is returned.

Please note that the equals method in String class in Java is case-sensitive. The complete code for equals method is reproduced here for reference:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
return false;
return true;
return false;

Also the compareTo method in String class is consistent with equals method.

Published at DZone with permission of its author, Sandeep Bhandari. (source)




Tomasz Nurkiewicz replied on Fri, 2012/11/23 - 2:55am



Mike P(Okidoky) replied on Fri, 2012/11/23 - 10:40am




public boolean equals(Object obj) 
    if (this == obj) 
        return true;

    if (obj instanceof String) 
        String str = (String)obj;

        if (count != str.count) 
            return false;

        if (hashCode() != str.hasCode())
            return false;

        for (int i = offset, j = strl.offset - i, k = offset + count; i < k; i++)
                return false;

        return true;
    return false;

And then my pet peeve is the awful K&R bracing style that Java developers use. Clearly, Alman style looks cleaner and you can browse up and down code and interpret it quicker and more efficiently. Saving vertical space should not be a motivation. I know this is a religious argument, but I can't for the life of me understand why people "like" K&R bracing style.

Stig Christensen replied on Sun, 2012/11/25 - 9:03am in response to: Mike P(Okidoky)

hasCode() would make it very expensive on large strings in many cases. 


Mike P(Okidoky) replied on Sun, 2012/11/25 - 10:57am in response to: Stig Christensen


Stig Christensen replied on Sun, 2012/11/25 - 11:54am in response to: Mike P(Okidoky)



If you do a lot of comparisons you would put them in a Hashset anyway, and String would then use the hashCode() as expected.

I think this is why String equals is written without hasCode(). It should not heart performance on a simple comparison.

Mike P(Okidoky) replied on Sun, 2012/11/25 - 12:32pm in response to: Stig Christensen



if (hash != 0 && anotherString.hash != 0 && hash != anotherString.hash) return false;


Wait... Arrays has public static boolean equals(char[] a, char[] a2)...
Doesn't accommodate an offset and isn't native...

Comment viewing options
