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

The == operator tests for reference , not values , equality that means check whether they are the same object. If two String variables point to the same object in memory, the comparison returns true. Otherwise, the comparison returns false.
"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

Java String equals() method check the original content (i.e. the same values) of the string. The equal() method returns true if the parameter is a String object that represents contain the exact same string of characters as this object.
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

Java String equalsIgnoreCase Compares two strings lexicographically , ignoring differences in case. Returns true if and only if the argument is a String object that represents the same sequence of characters as this object. When you want to test your strings for equality in case-insensitive manner, you can use the equalsIgnoreCase method of the String class. 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 when we need to determine the order of Strings lexicographically . It compares char values similar to the equals method. If the two strings are exactly the same, the compareTo method will return a value of 0 (result is = 0). It returns a positive (result is > 0) integer if the first String object follows the second string. The compareTo method returns a negative (result is < 0) integer if the first String object precedes the second string.
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