Compare two Strings in Java

Last Updated : 24 Aug, 2026

In Java, strings can be compared in different ways depending on whether we want to compare their content, ignore case, handle null values, or determine their lexicographical order.

  • equals() compares the content of two strings and is case-sensitive.
  • equalsIgnoreCase() compares string content while ignoring case.
Java
public class CompareStrings {
  
    public static void main(String[] args) {
      
        String s1 = "Hello";
        String s2 = "Geeks";
        String s3 = "Hello";

        // Comparing strings
        System.out.println(s1.equals(s2)); 
        System.out.println(s1.equals(s3));
    }
}

Output
false
true

Explanation: In the above example, the equals() method checks the content of s1 and s2. Since they differ, it returns false.It returns true for s1 and s3 because they have identical content.

Methods to Compare Strings

1. Using User-Defined Function

Define our own function to compare strings lexicographically. Define a function to compare values with the following conditions :

  • if (string1 > string2) it returns a positive value.
  • if both the strings are equal lexicographically i.e.(string1 == string2) it returns 0.
  • if (string1 < string2) it returns a negative value.
Java
public class CompareStrings {
  
    // User-defined function 
    // to compare two strings
    public static int compare(String s1, String s2) {
      
     
        // Uses compareTo method for 
        // lexicographical comparison 
        return s1.compareTo(s2);
    }

    public static void main(String[] args) {
      
        String s1 = "Java";
        String s2 = "Domain";

        // Call the compare function
        int res = compare(s1, s2);
        System.out.println("" + res); 
    }
}

Output
6

Explanation: In the above example, we get the output as 6, because the compareTo() method compares strings lexicographically, finding the difference between the first mismatched characters 'J' (Unicode 74) and 'D' (Unicode 68), resulting in 74 - 68 = 6.

 2. Using String.equalsIgnoreCase()

The String.equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string. This method returns true if the argument is not null and the contents of both the Strings are same ignoring case, else false.

Java
public class CompareStrings {
  
    public static void main(String args[]) {
      
        // Create two string objects with different cases
        String s1 = new String("Java");
        String s2 = new String("JAVA");

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

Output
true

Explanation: The equalsIgnoreCase() method treats "Java" and "JAVA" as equal because it ignores case sensitivity.

3. Using Objects.equals()

The Object.equals(Object a, Object b) method returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals() method of the first argument.

Java
import java.util.Objects;

public class CompareStrings {
  
    public static void main(String[] args) {
      
        // Create a string object 
        // and a null value
        String s1 = "Java";
        String s2 = null;

        System.out.println(Objects.equals(s1, s2)); 
        System.out.println(Objects.equals(null, null)); 
    }
}

Output
false
true

Note: Objects.equals() avoids NullPointerException by handling null values.

4. Using String.compareTo()

The String.compareTo() method compares strings lexicographically. It compares and returns the values as follows:

  • if (string1 > string2) it returns a positive value.
  • if both the strings are equal lexicographically i.e.(string1 == string2) it returns 0.
  • if (string1 < string2) it returns a negative value.
Java
public class CompareStrings {
  
    public static void main(String[] args) {
      
        // Define two strings for comparison
        String s1 = "Java";
        String s2 = "Domain";

        // The result will be a positive integer as 
        // "Java" comes after "Domain" lexicographically
        System.out.println(s1.compareTo(s2)); 
    }
}

Output
6

Note: NULL string can't be passed as a argument to compareTo() method.

Comment