Java String equals()

The java string equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.

  • Java String equals() method overrides the Object class equals() method implementation.

  • Since String is immutable, checking the equality of string to another object should be done using equals() method rather than == operator.

  • String equals() method always return boolean value, it doesn’t throw any exceptions.

  • The result of equals() method is true if and only if – the argument is not null, it’s a String object, represents same sequence of characters as this string.

  • Below is the code snippet showing implementation details of equals() method.

    public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String aString = (String)anObject; if (coder() == aString.coder()) { return isLatin1() ? StringLatin1.equals(value, aString.value) : StringUTF16.equals(value, aString.value); } } return false; }
    The method uses some of the internal classes and functions of String class, just have a look how it’s written in properly optimized way.

  • If you want case insensitive equality check, then you can use String equalsIgnoreCase() method. It’s signature is public boolean equalsIgnoreCase(String anotherString), note that here the argument is String object.

Internal implementation
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String) anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
Signature
public boolean equals(Object anotherObject)
Parameter

anotherObject : another object i.e. compared with this string.

Returns

true if characters of both strings are equal otherwise false.

Overrides

equals() method of java Object class.

Java String equals() method example
public class Test1 { public static void main(String args[]) { String s1="cprogramcoding"; String s2="cprogramcoding"; String s3="CPROGRAMCODING"; String s4="orical"; System.out.println(s1.equals(s2));//true because content and case is same System.out.println(s1.equals(s3));//false because case is not same System.out.println(s1.equals(s4));//false because content is not same } }


Output:
true false false
Java String equals() Method Example
public class Test1 { public static void main(String[] args) { String s1 = "cprogramcoding"; String s2 = "cprogramcoding"; String s3 = "cprogramcoding"; System.out.println(s1.equals(s2)); // True because content is same if (s1.equals(s3)) { System.out.println("both strings are equal"); }else System.out.println("both strings are unequal"); } }


Output:
true both strings are equal
Java String equals() Method Example
import java.util.ArrayList; public class Test1 { public static void main(String[] args) { String str1 = "Mukesh"; ArrayList list = new ArrayList<>(); list.add("Pavi"); list.add("Rama"); list.add("Ravi"); list.add("Prakash"); for (String str : list) { if (str.equals(str1)) { System.out.println("Rama is present"); } } } }


Output:
Rama is present



Instagram