VOOZH about

URL: https://www.geeksforgeeks.org/java/compare-two-strings-in-java/

⇱ Compare two Strings in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Compare two Strings in Java

Last Updated : 11 Jul, 2025

String in Java are immutable sequences of characters. Comparing strings is the most common task in different scenarios such as input validation or searching algorithms. In this article, we will learn multiple ways to compare two strings in Java with simple examples.

Example:

To compare two strings in Java, the most common method is equals(). This method compares the content of two strings for equality.


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.

Other Methods to Compare Strings in Java

1. Using User-Defined Function

We can 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.

Example:


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.

Example:


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.

Example:


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.

Example:


Output
6

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

Why Not Use == for String Comparison?

  • == operator checks reference equality i.e. whether two variables point to the same memory location.
  • The .equals() method is used to compare the content of two strings.
Comment