Java String equals() Method with Examples
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
The equals() method in Java is a fundamental operation when it comes to string comparison and equality checks.
Understanding how this method functions is paramount in Java programming. It serves as a cornerstone for determining whether two strings are identical, character by character, in the exact same order. Present within the String class equals() meticulously overrides the equals() method in the Object class. Accepting a string parameter empowers developers to scrutinize a string’s equality with the current string object.
By default, this method is case-sensitive, returning true only when the strings are a perfect match and false when they differ.
This article delves into the equals() method, exploring its signature, internal workings, and practical examples that vividly illustrate the string comparison and equality verification process in the Java programming language.
Method Signature
The method signature of Java equals() is:
public boolean equals(Object anotherObject)
It accepts an Object type parameter called anotherObject, which is the string with which to compare the current string. The method returns a boolean value – true if the strings are equal or false if they are not.
Internal Implementation
Internally, the equals() method compares the characters of both strings. It checks if the parameter string and the current string have the same length. If yes, it then compares characters one by one from the start to the end of both strings.
By default, the comparison is case-sensitive. If any character mismatches, the method returns false immediately. Only if all characters match does it return true.
The equals() method properly handles different data types. If the parameter is not a string, it simply returns false.
Java String equals() Method Examples
Let’s look at a few examples to better understand the usage of equals().
Example 1:
public class StringEqualityExample { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; String str3 = "World"; System.out.println(str1.equals(str2)); // true System.out.println(str1.equals(str3)); // false } }
Output:
true
false
Here we have declared three string variables str1, str2, and str3. We compare str1 and str2 using equals(), which prints true since both refer to the same string “Hello”.
When str1 and str3 are compared, it prints false as the strings are not equal.
Example 2:
public class CaseSensitiveComparisonExample { public static void main(String[] args) { String str1 = "Hello"; String str2 = "hello"; if (str1.equals(str2)) { System.out.println("Strings are equal"); } else { System.out.println("Strings are not equal"); } } }
Output:
Strings are not equal
In this example, str1 and str2 contain the same text but with different cases.
When compared using equals(), it prints “Strings are not equal” since the comparison is case-sensitive by default.
Example 3:
import java.util.ArrayList; public class ArrayListStringComparisonExample { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Hello"); list.add("World"); String str = "Hello"; if (list.contains(str)) { System.out.println(str + " found in ArrayList"); } else { System.out.println(str + " not found in ArrayList"); } } }
Output:
Hello found in ArrayList
Here, we declare an ArrayList of strings and check if a given string str is present in the list using contains().
Since the list contains “Hello”, it prints “Hello found in ArrayList”. The contains() method internally uses equals() to compare the elements.
Example 4:
public class ObjectTypeComparisonExample { public static void main(String[] args) { String str1 = "Hello"; Integer num = 5; System.out.println(str1.equals(num)); // false String str2 = "World"; System.out.println(str1.equals(str2)); // false } }
Output:
false
false
This example demonstrates how equals() handles a different data type. When str1 is compared with integer num, it returns false.
Similarly, when two different string objects, str1 and str2, are compared, equals() returns false.
Conclusion
In summary, Java’s equals() method serves as a reliable sentinel in the quest for string equality. It meticulously inspects strings character by character, rigorously adhering to a case-sensitive criterion, thereby affirming their identical nature.
This article has provided a comprehensive view of the equals() method, elucidating its signature, inner workings, and practical applications through illustrative examples.
Whether comparing strings, inspecting ArrayLists filled with strings, or exploring comparisons with other object types, the equals() method is an invaluable asset in your Java programming toolkit.
By effectively harnessing this method, you can confidently and accurately determine the equality of strings, enriching your ability to craft robust and precise Java programs.
Did we exceed your expectations?
If Yes, share your valuable feedback on Google