Java String equals() Method (original) (raw)

Last Updated : 23 Jul, 2025

String **equals() method in Java compares the content of two strings. It compares the value's character by character, irrespective of whether two strings are stored in the same memory location. The String equals() method overrides the equals() method of the object class.

**Example: Basic Comparision

Java `

class Geeks { public static void main(String[] args) {

    String str1 = "Learn Java";
    String str2 = "Learn Java";
    String str3 = "Learn Kotlin";
    boolean result;

    // Comparing str1 with str2
    result = str1.equals(str2);
    System.out.println(result);

    // Comparing str1 with str3
    result = str1.equals(str3);
    System.out.println(result);

    // Comparing str3 with str1
    result = str3.equals(str1);
    System.out.println(result);
  
}

}

`

**Syntax

string.equals(object)

**Return Type: The return type for the equals method is Boolean.

**Case-Insensitive Comparison of Strings

It compares the difference between uppercase and lowercase characters. We compare three strings using the equalsIgnoreCase() method of str1, str2, and str3 to check case-insensitive comparison.

**Example:

Java `

// Working equalsIgnoreCase() Method import java.io.*;

public class Geeks { public static void main(String[] args) { // Define three strings String str1 = "Hello"; String str2 = "hello"; String str3 = "WORLD";

    // Comparison between str1 and str2
    System.out.println("str1.equals(str2): "+ str1.equals(str2)); 

    // Comparison between str1 and str2(Case Insensitive)
    System.out.println("str1.equalsIgnoreCase(str2): "+ str1.equalsIgnoreCase(str2)); 

    // Comparison between str2 and str3
    System.out.println("str2.equalsIgnoreCase(str3): "+ str2.equalsIgnoreCase(str3)); 
}

}

`

Output

str1.equals(str2): false str1.equalsIgnoreCase(str2): true str2.equalsIgnoreCase(str3): false