Open In App
Related Articles

Compare two Strings in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The string is a sequence of characters. In Java, objects of String are immutable which means they are constant and cannot be changed once created. 

Methods to Compare Strings in Java

Below are 5 ways to compare two Strings in Java:

  1. Using user-defined function
  2. Using String.equals()
  3. Using String.equalsIgnoreCase()
  4. Using Objects.equals()
  5. Using String.compareTo()

1. Using user-defined function: 

Define a function to compare values with the following conditions :

  1. if (string1 > string2) it returns a negative value.
  2. if both the strings are equal lexicographically i.e.(string1 == string2) it returns 0.
  3. if (string1 < string2) it returns a positive value.

2. Using String.equals() :

In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false. 

Syntax:

str1.equals(str2);

Here str1 and str2 both are the strings that are to be compared. 

Examples:

Input 1: GeeksforGeeks
Input 2: Practice
Output: false
Input 1: Geeks
Input 2: Geeks
Output: true
Input 1: geeks
Input 2: Geeks
Output: false

Program: 

Java

// Java program to Compare two strings
// lexicographically
public class GFG {
    public static void main(String args[])
    {
        String string1 = new String("Geeksforgeeks");
        String string2 = new String("Practice");
        String string3 = new String("Geeks");
        String string4 = new String("Geeks");
        String string5 = new String("geeks");
  
        // Comparing for String 1 != String 2
        System.out.println("Comparing " + string1 + " and "
                           + string2 + " : "
                           + string1.equals(string2));
  
        // Comparing for String 3 = String 4
        System.out.println("Comparing " + string3 + " and "
                           + string4 + " : "
                           + string3.equals(string4));
  
        // Comparing for String 4 != String 5
        System.out.println("Comparing " + string4 + " and "
                           + string5 + " : "
                           + string4.equals(string5));
  
        // Comparing for String 1 != String 4
        System.out.println("Comparing " + string1 + " and "
                           + string4 + " : "
                           + string1.equals(string4));
    }
}

                    

Output
Comparing Geeksforgeeks and Practice : false
Comparing Geeks and Geeks : true
Comparing Geeks and geeks : false
Comparing Geeksforgeeks and Geeks : false



3. 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. Syntax:

str2.equalsIgnoreCase(str1);

Here str1 and str2 both are the strings which are to be compared. 

Examples:

Input 1: GeeksforGeeks
Input 2: Practice
Output: false
Input 1: Geeks
Input 2: Geeks
Output: true
Input 1: geeks
Input 2: Geeks
Output: true

Program: 

Java

// Java program to Compare two strings
// lexicographically
public class GFG {
    public static void main(String args[])
    {
        String string1 = new String("Geeksforgeeks");
        String string2 = new String("Practice");
        String string3 = new String("Geeks");
        String string4 = new String("Geeks");
        String string5 = new String("geeks");
  
        // Comparing for String 1 != String 2
        System.out.println(
            "Comparing " + string1 + " and " + string2
            + " : " + string1.equalsIgnoreCase(string2));
  
        // Comparing for String 3 = String 4
        System.out.println(
            "Comparing " + string3 + " and " + string4
            + " : " + string3.equalsIgnoreCase(string4));
  
        // Comparing for String 4 = String 5
        System.out.println(
            "Comparing " + string4 + " and " + string5
            + " : " + string4.equalsIgnoreCase(string5));
  
        // Comparing for String 1 != String 4
        System.out.println(
            "Comparing " + string1 + " and " + string4
            + " : " + string1.equalsIgnoreCase(string4));
    }
}

                    

Output
Comparing Geeksforgeeks and Practice : false
Comparing Geeks and Geeks : true
Comparing Geeks and geeks : true
Comparing Geeksforgeeks and Geeks : false



4. Using Objects.equals() : 

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. Syntax:

public static boolean equals(Object a, Object b)

Here a and b both are the string objects which are to be compared. 

Examples:

Input 1: GeeksforGeeks
Input 2: Practice
Output: false
Input 1: Geeks
Input 2: Geeks
Output: true
Input 1: null
Input 2: null
Output: true

Program: 

Java

// Java program to Compare two strings
// lexicographically
  
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        String string1 = new String("Geeksforgeeks");
        String string2 = new String("Geeks");
        String string3 = new String("Geeks");
        String string4 = null;
        String string5 = null;
  
        // Comparing for String 1 != String 2
        System.out.println(
            "Comparing " + string1 + " and " + string2
            + " : " + Objects.equals(string1, string2));
  
        // Comparing for String 2 = String 3
        System.out.println(
            "Comparing " + string2 + " and " + string3
            + " : " + Objects.equals(string2, string3));
  
        // Comparing for String 1 != String 4
        System.out.println(
            "Comparing " + string1 + " and " + string4
            + " : " + Objects.equals(string1, string4));
  
        // Comparing for String 4 = String 5
        System.out.println(
            "Comparing " + string4 + " and " + string5
            + " : " + Objects.equals(string4, string5));
    }
}

                    

Output
Comparing Geeksforgeeks and Geeks : false
Comparing Geeks and Geeks : true
Comparing Geeksforgeeks and null : false
Comparing null and null : true



5. Using String.compareTo() for Comparing Two Strings

Syntax of String compareTo()

int str1.compareTo(String str2)

Working: It compares and returns the following values as follows:

  1. if (string1 > string2) it returns a positive value.
  2. if both the strings are equal lexicographically i.e.(string1 == string2) it returns 0.
  3. if (string1 < string2) it returns a negative value.

Below is the implementation of the above method

Java

// Java program to Compare two strings
// Using String.compareTo() 
  
import java.util.*;
  
// Diver Class
public class GFG {
    public static void check(String string1, String string2)
    {
        if (string1.compareTo(string2)!=0) {
            System.out.println(string1 + " " + string2
                               + " : Not Equal");
        }
        else {
            System.out.println(string1 + " " + string2
                               + " : Equal");
        }
    }
  
    // main function
    public static void main(String args[])
    {
        String string1 = new String("Geeksforgeeks");
        String string2 = new String("Geeks");
        String string3 = new String("Geeks");
        String string4 = " ";
        String string5 = " ";
  
        // Comparing for String 1 != String 2
        check(string1, string2);
  
        // Comparing for String 2 = String 3
        check(string2, string3);
  
        // Comparing for String 1 != String 4
        check(string1, string4);
  
        // Comparing for String 4 = String 5
        check(string4, string5);
    }
}

                    

Output
Geeksforgeeks Geeks : Not Equal
Geeks Geeks : Equal
Geeksforgeeks   : Not Equal
    : Equal



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

To know more about the topic refer to the String.compareTo() article. 

Why not use == for the comparison of Strings?

In general, both equals() and “==” operators in Java are used to compare objects to check equality but here are some of the differences between the two:

  1. The main difference between the .equals() method and == operator is that one is the method and the other is the operator.
  2. One can use == operators for reference comparison (address comparison) and the .equals() method for content comparison.
    • Both s1 and s2 refer to different objects.
    • When one uses == operator for the s1 and s2 comparison then the result is false as both have different addresses in memory.
    • Using equals, the result is true because it’s only comparing the values given in s1 and s2.

To know more about the topic refer to the comparison of strings article.



Last Updated : 12 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads