Java | ==, equals(), compareTo(), equalsIgnoreCase() and compare()

Last Updated : 22 Jan, 2026

In Java, there are multiple ways to compare two String objects. Each method serves a different purpose and behaves differently based on whether reference comparison, content comparison, case sensitivity, or locale-specific rules are required.

The commonly used string comparison techniques are:

  • Using == operator
  • Using equals() method
  • Using compareTo() method
  • Using compareToIgnoreCase() method
  • Using compare() method

Method 1: using == operator

The == operator compares object references, not the actual content of the strings. It returns true only if both references point to the same memory location.Strings created without the new keyword are stored in the String Constant Pool, so they may refer to the same object. 

Java
class GFG {
    public static void main(String[] args)
    {
        String s1 = "A";
        String s2 = "A";
        String s3 = "A";
        String s4 = new String("A");

        System.out.println(s1 == s2);
        System.out.println(s1 == s3);
        System.out.println(s2 == s3);
        System.out.println(s1 == s4);
    }
}

Output
true
true
true
false

Explanation:

  • s1, s2, and s3 are string literals, so they refer to the same object in the String Constant Pool.
  • The == operator compares memory references, not string content.
  • Comparisons between s1, s2, and s3 return true because they point to the same reference.
  • s4 is created using the new keyword, so it refers to a different object in memory.
  • Therefore, s1 == s4 returns false.

Method 2: Using equals() method

The equals() method compares the content of two strings. It returns true if both strings have the same sequence of characters.

JAVA
class GFG {
    public static void main(String[] args)
    {
        String s1 = "A";
        String s2 = "A";
        String s3 = "a";
        String s4 = new String("A");

        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s2.equals(s3));
        System.out.println(s1.equals(s4));
    }
}

Output
true
false
false
true

Explanation:

  • equals() checks the actual characters in the strings, not their memory references.
  • s1.equals(s2) returns true because both contain "A".
  • s1.equals(s3) and s2.equals(s3) return false due to case difference ("A" vs "a").
  • s1.equals(s4) returns true because both strings have the same content, even though s4 is created using new.

Method 3: Using compareTo() method

The compareTo() method compares strings lexicographically based on their Unicode values.

It returns:

  • 0 if both strings are equal
  • A negative value if the first string is smaller
  • A positive value if the first string is greater

JAVA
class GFG {
    public static void main(String[] args)
    {
        String s1 = "A";
        String s2 = "A";
        String s3 = "a";
        String s4 = new String("A");

        System.out.println(s1.compareTo(s2));
        System.out.println(s1.compareTo(s3));
        System.out.println(s3.compareTo(s2));
        System.out.println(s1.compareTo(s4));
    }
}

Output
0
-32
32
0

Explanation:

  • compareTo() returns 0 if strings are equal, a negative value if the first string is smaller, and a positive value if it is greater.
  • s1.compareTo(s2) - 0 because both are "A".
  • s1.compareTo(s3) - -32 because "A" has a lower Unicode value than "a".
  • s3.compareTo(s2) - 32 because "a" has a higher Unicode value than "A".
  • s1.compareTo(s4) - 0 because both strings have the same content.

Method 4: Using equalsIgnoreCase() method

The equalsIgnoreCase() method compares string contents without considering case differences. It is a case-insensitive version of the equals() method.

JAVA
class GFG {
    public static void main(String[] args)
    {
        String s1 = "A";
        String s2 = "A";
        String s3 = "a";
        String s4 = new String("A");

        System.out.println(s1.equalsIgnoreCase(s2));
        System.out.println(s1.equalsIgnoreCase(s3));
        System.out.println(s2.equalsIgnoreCase(s3));
        System.out.println(s1.equalsIgnoreCase(s4));
    }
}

Output
true
true
true
true

Explanation:

  • equalsIgnoreCase() checks string content without considering uppercase or lowercase.
  • s1.equalsIgnoreCase(s2) - true because both are "A".
  • s1.equalsIgnoreCase(s3) and s2.equalsIgnoreCase(s3) - true because "A" and "a" are considered equal ignoring case.
  • s1.equalsIgnoreCase(s4) - true because both have the same content, even though s4 is created using new.

Method 5: Using compare() method

For locale-sensitive string comparison, Java provides the Collator class from the java.text package.It allows comparison based on language-specific rules.

JAVA
import java.text.Collator;
class GFG {
    public static void main(String[] args)
    {
        Collator collator = Collator.getInstance();

        String s1 = "A";
        String s2 = "A";
        String s3 = "a";
        String s4 = new String("A");

        System.out.println(collator.compare(s1, s2));
        System.out.println(collator.compare(s1, s3));
        System.out.println(collator.compare(s3, s2));
        System.out.println(collator.compare(s1, s4));
    }
}

Output
0
1
-1
0

Explanation:

  • Collator allows comparison of strings according to language-specific rules.
  • collator.compare(s1, s2) - 0 because "A" and "A" are equal.
  • collator.compare(s1, s3) - 1 because "A" is considered greater than "a" in the default locale rules.
  • collator.compare(s3, s2) - -1 because "a" is considered smaller than "A".
  • collator.compare(s1, s4) - 0 because both strings have the same content, even though s4 is a different object.
Comment