java.lang.String equals()


This java tutorial shows how to use the equals() method of java.lang.String class. This method returns boolean data type which checks if the String is equals to the object input parameter on this method. This generally checks if the string is the same character sequence as that of the object parameter. This method is an override from of the equals method inherited from Object class.

Method Syntax :

public boolean equals(Object anObject)

Parameter Input :

  • anObject – this is a parameter object which we are trying to compare on our String.

Method Returns :

This method returns boolean datatype that throws true if the String and Object parameter input is the same in terms of character sequence, false if otherwise.

Java Code Example :

This example source code demonstrates the use of equals() method of String class. This checks the equality of two strings by comparing it with equals method of String class. As you have already noticed we have used the conversion of char array to String.


 * This is example source code to check for String equality

public class StringEqualsDemo {

    public static void main(String[] args) {
        String name = "Grace";
        // instantiate a new character array
        char[] value = new char[]{'G','r','a','c','e'};
        // convert the character array to String
        String param = new String(value);
        // check for String equality
            System.out.println(name +" is equal to "+param);
            System.out.println(name +" is NOT equal to "+param);


Sample Output :

Running the equals() example source code will give you the following output

java string equals ignore case method example

java string equals ignore case method example

Suggested Reading List :