Strings in Java are considered an immutable object. The term string is also referred to as “a sequence of characters”. The comparison of Strings is required in some real-world scenarios. Java is a programming language that comprises multiple methods through which we can compare the strings in Java. These methods are String.equals(), String.equalsIgnoreCase(), Object.equals(), String.compareTo(), and String.regionMatches().
In this article, the string equals() method in Java will be demonstrated in detail.
How to Use/Implement String equals() Method in Java?
The string equals() method in Java checks the two strings in such a manner that the character available in the first declared string is compared with the character available in the second declared string. A boolean value of “true” is returned if all the characters from the first string match with the second string, otherwise “false” is returned.
Syntax
Below is the syntax of the equals() method.
public boolean equals(FirstString SecondString)
Return Value
The boolean value will be returned as true if strings are equal otherwise false.
Example 1: Implementation of String equals() Method in Java
The code below implements the equals() method and prints the result accordingly.
//Class for the equals method
class equalsmethod {
//The main class of Java
public static void main(String[] args) {
//Declare the strings
String a = "Welcome";
String b = "Welcome";
String c = "To";
String d = "Java";
//If, else statement to check whether the strings are equal or not.
if (a.equals(c)) {
//The output statement to print the results.
System.out.println("Strings a and c are equal so the value is TRUE");
} else
System.out.println("Strings a and c are unequal so the value is FALSE");
//if, else statement to check whether the strings are equal or not.
if (a.equals(b)) {
//The output statement to print the results.
System.out.println("Strings a and b are equal so the value is TRUE");
} else
System.out.println("Strings a and b are unequal so the value is FALSE");
}
}
In the above Java code:
- A class is declared as “equalsmethod” to implement the string equals() method.
- Four strings are declared as a,b,c, and d.
- The if-else statement compares the two strings, a and c with each other.
- The next if-else statement compares a and b with each other and prints the result with the boolean values.
Output
The below output shows that the strings a and c are equal while the other two strings a and b are not equal.

Example 2: Using equals() Method With ArrayList and for Loop.
The code below describes the working of the equals() method using the ArrayList and a for loop.
//Import the required package
import java.util.ArrayList;
//Declare the class for the equals() method
public class equalsmethod {
//Main class of Java
public static void main(String[] args) {
String s1 = "Python";
ArrayList<String> cslist = new ArrayList<>();
//Enter elements in the ArrayList
cslist.add("C++");
cslist.add("Python");
cslist.add("Java");
//Use a for loop to check whether the string is present or not.
for (String s : cslist) {
if (s.equals(s1)) {
System.out.println("Python is in the list");
}
}
}
}
In the above code:
- A string s1 is declared in the Main class.
- Three elements are added in the ArrayList using the add() method.
- The for loop declares the ArrayList and the If statement compares the string with the element in the ArrayList.
- If the element and the entered string are the same the output is printed accordingly.
Output
The below output shows that the string Python is present in the ArrayList.

That’s all about string comparison using Java’s equals() method.
Conclusion
The string equals() method compares the characters in two strings and returns a boolean value of true if the strings are equal otherwise false. Various methods are available that compare the strings in Java. In this article, the equals() method to compare the two strings is implemented in an effective manner.