Comparing two strings means checking if two strings are equal or not. This article will cover different ways to compare strings in java with example programs.

Method 1: Using == operator
Java’s comparison operator == can be used to compare two string literals. It compares references and returns true if reference to both the strings are same, false if they are not. Example,

String first = "test string"; 
String second = "test string"; 
String unequal = "not equal"; 
System.out.println(first == second);  // prints true 
System.out.println(first == third);   //prints false

In the above example you must notice that reference variables first and second are not same(point to different strings) still their comparison returns true.

A string created using direct initialization is referred as String literal.
Example, String s = "codippa";
A string created using String class constructor is a String object.
Example, String s = new String("codippa");

Reason is, JVM maintains a string pool where string values are stored.
When a string literal is created, it is placed in that pool. When a new string literal is created, JVM first searches this pool.
If a string with the same value is already present in the pool, then new string reference is also pointed to it as shown below.
JVM string pool
Thus, when they are compared, the result is true. If one of the string reference is assigned to a different value, a new string is created in the pool and the reference is updated.
Now if they are compared, the result will be false.

Method 2: Using equals() method
String class has an equals() method which takes another string object as argument. This method when invoked on a string object compares this string object with the object supplied to it as argument.
It compares
1. References of both the objects.
2. Values of both string objects.
If any of the above are same, that is, both reference variables point to same object or their values are same, then it returns true, false otherwise.
Note that equals() method will return true if values of two string objects are same and even if they point to different object. Example,

String first = "test string"; 
String second = new String("test string"); 
String unequal = "not equal"; 
System.out.println(first == second);  // prints false 
System.out.println(first.equals(second));  // prints true 
System.out.println(first.equals(third));   // prints false

In the above example, first and second string reference variables point to different strings but their values are same.
Thus, when compared with == operator, the result is false while when they are compared with equals(), the result is true.
Similarly, when first and unequal are compared, the result is false because their values are different.

Remember that when comparing string objects using equals, a null check should be performed first. A java.lang.NullPointerException will be raised if the string object on which equals is called is null.
Method 3: Using equalsIgnoreCase() method
Comparison using equals() method is case sensitive. That is, test string and Test string are different values for equals().
For case insensitive comparison, using equalsIgnoreCase() method instead as shown below.

String first = "test string"; 
String second = "Test string"; 
System.out.println(first.equals(second));  // prints false 
System.out.println(first.equalsIgnoreCase(second));  // prints true 

Look, equals() treats them as different strings though they only differ in case while equalsIgnoreCase() compares their values irrespective of their case.

Method 4: Using Objects class
java.util.Objects class has an equals() method which can be used to compare two strings.
It takes two objects as arguments and compares them.
It is similar to equals() method in that it also compares
1. References of both the objects.
2. Values of both string objects.
If any of the above are same, it considers both the objects to be same and returns true. Example,

String first = "test string"; 
String second = new String("test string"); 
System.out.println(first.equals(second)); 
boolean isEqual = Objects.equals(first, second); 
System.out.println(isEqual); // prints true

There is no need to perform null check when using Objects.equals() as it internally checks for null value on first object.
Note that this method is static and so there is no need to create an object to invoke this method.
Objects class was added in java 7.
Method 5: Using compareTo()
Java string compareTo() method can be used to check if two strings are equal or not.
compareTo() accepts a string as argument and compares it with the string it is invoked on. It returns 0 if both the strings are same or equal. Example,

String s1 = "hero";
String s2 = "hero";
if(s1.compareTo(s2) == 0){
  System.out.println("Strings are equal");
} else {
  System.out.println("Strings are not equal");
}

Method 6: Using compareToIgnoreCase()
compareTo() will not work when strings being compared are of different case even though their values are same. So, “hero” and “Hero” are different strings for compareTo().
For case insensitive matching, use compareToIgnoreCase() method as shown below.

String s1 = "hero";
String s2 = "Hero";
if(s1.compareTo(s2) == 0){ 
  System.out.println("Strings are equal"); 
} else { 
  System.out.println("Strings are not equal"); 
}
if(s1.compareToIgnoreCase(s2) == 0){ 
  System.out.println("Strings are equal"); 
} else { 
  System.out.println("Strings are not equal"); 
}

which prints

Strings are not equal
Strings are equal

Method 7: Using Apache Commons
Apache Commons library can be used to compare two strings to check their equality.
It has a StringUtils class having a compare() method which takes two string arguments and returns 0 if the strings are equal. Example,

String s1 = "hero"; 
String s2 = "hero";
if(StringUtils.compareTo(s2) == 0){
  System.out.println("Strings are equal");
} else {
  System.out.println("Strings are not equal");
}

compare() is for case sensitive comparison and treats strings with same values but different case as separate. For case insensitive comparison, use its compareIgnoreCase() method, which works same as compare().

Both compare() and compareIgnoreCase() methods of Apache Commons make use of compareTo() method of java String class internally.
When to use which
1. When one string is created using direct assignment(as String first = "test string";) and another string is created with String class constructor(as String second = new String("test string");), comparison operator(==) will always return false irrespective of their values.
You should use other methods in this case.
2. When both strings are created using String class constructor, comparison(==) operator will always return false irrespective of their values. You should use other methods in this case too.
3. When both strings are created using direct assignment, it is better to use comparison operator(==).
4. If your application is already using Apache Commons, then use its compare() method. Do not add a dependency only for string comparison.
5. You can not use Objects.equals() method if you are running below java 7.
Hope the article was useful.