The Java equals() method is used to check whether the two strings are equal or not.

Java equals() method:

As we discussed in the earlier article that the differences between equals and == operator, the equals() method checks the content of the strings to confirm whether two strings are equal or not.

The equals() method takes a string as an argument or parameter, compares both strings, and returns a boolean value(true or false). Here are a few examples that explain how the equals() method works:

Method signature:

The signature for an equals() method is as shown below.

public boolean equals(String str)

Method parameters and return type:

Return type: It returns a boolean value(true or false).

Parameters: It takes a string as a parameter.

Throws: It throws NullPointerException if the string is null.

Example 1:

In this example, we have initialized three strings, and we have compared whether the strings are equal or not.

Source code:

public class Example1 {
    
    public static void main(String[] args) {
        String str1 = "Java Programming";
        String str2 = "Python Programming";
        String str3 = "Python Programming";
        
        if(str1.equals(str2)){
            System.out.println("They are equal");
        } else{
            System.out.println("They are not equal");
        }
        
        if(str2.equals(str3)){
            System.out.println("They are equal");
        } else{
            System.out.println("They are not equal");
        }
    }
}

Output:

run: 
They are not equal They are equal 
BUILD SUCCESSFUL (total time: 1 second)

Example 2:

You can also use equals() method in Java for linear searching. In this example, we have used equals() method for searching “Java” in the array of strings.

Source code:

public class Example2 {
    
    public static void main(String[] args) {
        
        String strArray[] ={"Java", "C++", "Python", "Java"};
        
        for(int i=0; i<strArray.length; i++){
            if(strArray[i].equals("Java")){
                System.out.println("Java is at at index: "+i);
            }
        }
    }
}

Output:

run:
Java is at at index: 0
Java is at at index: 3
BUILD SUCCESSFUL (total time: 0 seconds)

Example 3:

If you try to compare a string with other data types using the equals() method, it will always return false. The best solution for this problem is to convert other data types to strings.

Source code:

public class Example3 {

    public static void main(String[] args) {
        String str1 =  new String("i");
        String str2 = new String("123");
        String str3 = new String("false");

        Character character = 'i';
        Integer num = 123;
        Boolean check = false;

        System.out.println(str1.equals(character));
        System.out.println(str2.equals(num));
        System.out.println(str3.equals(check));

        System.out.println(str1.equals(character.toString()));
        System.out.println(str2.equals(num.toString()));
        System.out.println(str3.equals(check.toString()));
    }
}

Output:

run:
false
false
false
true
true
true
BUILD SUCCESSFUL (total time: 0 seconds)

References:

Happy Learning 🙂