String Comparison in Java

Java String class has a number of methods for comparing strings. The following are the some of the frequently used methods:

  1. == operator
  2. equals() method
  3. equalsIgnoreCase
  4. compareTo() method

Using == operator

In Java, the == operator is used to test for reference equality rather than value equality when comparing objects, including strings. When comparing two String variables using the == operator, it checks if they refer to the exact same object in memory.

If two String variables point to the same object in memory, the comparison using == will return true, indicating that they are the same object. This means they have the same memory address and are essentially the same instance.

However, if the two String variables are separate instances with different memory addresses, even if they contain the same sequence of characters, the comparison using == will return false. This is because the operator checks for identity rather than the actual content of the strings.

To compare the content or values of two strings, you should use the equals() method or other appropriate methods provided by the String class. The equals() method compares the characters within the strings and returns true if they have the same sequence of characters, regardless of whether they are the same object in memory.

"Java" == "Java" //true

Here, the literals are interned by the compiler and thus refer to the same object

new String("Java") == "Java" // false

Above two String variables point to the different object in memory

new String("Java") == new String("Java") // false

Above two String variables point to the different object in memory

It is important to note that the '==' operator does not compare the content inside the String objects. It only compares the references the 2 Strings are pointing to.

Example
class TestClass{ public static void main (String[] args){ //refer to the same object and return true if( "Java" == "Java" ){ System.out.println("Statement is true"); }else{ System.out.println("Statement is false"); } //point to the different object in memory return false if(new String("Java") == "Java"){ System.out.println("Statement is true"); }else{ System.out.println("Statement is false"); } //point to the different object in memory return false if(new String("Java") == new String("Java") ){ System.out.println("Statement is true"); }else{ System.out.println("Statement is false"); } } }
Output
Statement is true Statement is false Statement is false

String equals() method

The equals() method of the String class is used to check the equality of the original content, or the actual values, of two strings. The method compares the characters within the strings and returns true if the parameter passed to it is a String object that represents the exact same string of characters as the object on which the method is called.

The equals() method performs a character-by-character comparison of the strings and checks if they have the same sequence of characters. If the parameter string has the same characters in the same order as the original string, the method returns true, indicating that they are equal in terms of content.

It's important to note that the equals() method is case-sensitive. This means that the comparison takes into account the exact characters, including their case (uppercase or lowercase). If the characters or their case differ between the two strings, the method will return false.

Using the equals() method allows you to determine if two strings have the same content, regardless of their memory address or reference. It is a common practice to use equals() for content comparison when working with strings in Java.

Objects.equals("Java", new String("Java")) //true

If you want to test whether two strings have the same value you will probably want to use Objects.equals() .

Example

class TestClass{ public static void main (String[] args) { String str1 = "Java"; String str2 = "Java"; String str3 = "ASP"; String str4 = "JAVA"; String str5 = new String("Java"); //both are equal and return true if(str1.equals(str2)){ System.out.println("Statement is true"); }else{ System.out.println("Statement is false"); } //both are not equal and return false if(str1.equals(str3)){ System.out.println("Statement is true"); }else{ System.out.println("Statement is false"); } //both are not equal and return false if(str1.equals(str4)){ System.out.println("Statement is true"); }else{ System.out.println("Statement is false"); } //both are equal and return true if(str1.equals(str5)){ System.out.println("Statement is true"); }else{ System.out.println("Statement is false"); } } }
Output
Statement is true Statement is false Statement is false Statement is true

String equalsIgnoreCase

The equalsIgnoreCase() method of the String class is used to compare two strings lexicographically, ignoring differences in case. It returns true if and only if the argument passed to it is a String object that represents the same sequence of characters as the original string, regardless of differences in case.

The equalsIgnoreCase() method performs a comparison between the characters of the strings in a case-insensitive manner. It disregards whether the characters are uppercase or lowercase, treating them as equal if they have the same alphabetical value. This is particularly useful when you want to test the equality of strings without considering differences in case.

By using the equalsIgnoreCase() method, you can determine if two strings have the same content, regardless of the case of their characters. If the strings have the same sequence of characters, regardless of whether they are uppercase or lowercase, the method will return true. Otherwise, it will return false.

This method is helpful when you need to perform case-insensitive comparisons, such as when validating user input or when dealing with data that may have inconsistent casing. It provides a convenient way to compare strings without having to convert them to a specific case beforehand.

It's important to note that the equalsIgnoreCase() method only compares the characters in the strings and does not consider other linguistic or locale-specific factors. It solely focuses on the equality of the character sequences, ignoring case differences.

Example
class TestClass{ public static void main (String[] args){ String str1 = "Java"; String str2 = "JAVA"; //return true because both are equal in case-insensitive manner if(str1.equalsIgnoreCase(str2)){ System.out.println("Statement is true"); }else{ System.out.println("Statement is false"); } //returns false because case-sensitive manner if(str1.equals(str2)){ System.out.println("Statement is true"); }else{ System.out.println("Statement is false"); } } }
Output
Statement is true Statement is false

String compareTo() method

The compareTo() method is used to compare two strings lexicographically, based on their Unicode values of the characters. It allows us to determine the order of strings in terms of their alphabetical or numerical sequence.

The compareTo() method compares the characters of two strings, starting from the first character. It returns an integer value that indicates the relationship between the two strings. Here's how the return value of compareTo() can be interpreted:

If the two strings are exactly the same (i.e., all characters are identical), the compareTo() method returns 0. This means that the strings are equal in terms of their lexicographic order.

If the first string comes after the second string in lexicographic order, the compareTo() method returns a positive integer value. This value represents the difference between the Unicode value of the first differing character in the two strings.

If the first string comes before the second string in lexicographic order, the compareTo() method returns a negative integer value. Similarly, this value represents the difference between the Unicode value of the first differing character in the two strings.

result1 == result2 :returns 0 result1 > result2 :returns positive value result1 < result2 :returns negative value
Example
class TestClass{ public static void main (String[] args) { String str1 = "Java"; String str2 = "Java"; String str3 = "ASP"; int val = 0; val = str1.compareTo(str2); System.out.println(val); val = str1.compareTo(str3); System.out.println(val); val = str3.compareTo(str1); System.out.println(val); } }
Output
0 9 -9

Conclusion

It's important to note that the compareTo() method compares strings based on their Unicode values, which may differ from the expected ordering in specific locales or language-specific collations. Therefore, if you need to perform locale-sensitive or language-specific comparisons, you should consider using the Collator class or locale-specific comparison methods.