Java compare two strings

Let’s learn java compare two strings.

Compare two strings in java

In java, objects of String are immutable. Meaning, once created it can’t be changed, they are constant. Below are few ways to compare strings in java.

String compare “==” operator

This operator don’t compare values it compares only references.

public class EqualOperatorDemo 
{
   public static void main(String[] args) 
   {
      String str1 = "Helloworld"; 
      String str2 = "Helloworld"; 
      String str3 = new String("Helloworld"); 
      System.out.println(str1 == str2); 
      System.out.println(str1 == str3);
   }
}



Output:

true
false


compareTo method : this method compares two strings lexicographically. compareTo() method compares two strings and if it returns positive value then str1 > str2, if it returns negative value then str1 < str2 and if it returns 0 then str1 == str2.

public class CompareToDemo 
{
   public static void main(String[] args) 
   {
      String str1 = new String("HelloWorld"); 
      String str2 = new String("Flower"); 
      String str3 = new String("Hello"); 
      String str4 = new String("Hello");
      // compare str1 < str2 
      System.out.println("Compare " + str1 + " and " + str2 + " - " + str1.compareTo(str2));
      // compare str3 = str4 
      System.out.println("Compare " + str3 + " and " + str4 + " - " + str3.compareTo(str4));
      // compare str1 > str4 
      System.out.println("Compare " + str1 + " and " + str4 + " - " + str1.compareTo(str4));
   }
}



Output:

Compare HelloWorld and Flower – 2
Compare Hello and Hello – 0
Compare HelloWorld and Hello – 5

Also read – difference between == and equals method in java


Java String.equals() method

String equals() method compares two strings based on data. Equals method returns true if the given object represents a String equivalent to this string, false otherwise.

Let’s see an example,

public class EqualsMethodDemo 
{
   public static void main(String[] args) 
   {
      String str1 = new String("HelloWorld"); 
      String str2 = new String("Flower"); 
      String str3 = new String("Hello"); 
      String str4 = new String("Hello"); 
      String str5 = new String("hello");
      // compare str1 != str2 
      System.out.println("Compare " + str1 + " and " + str2 + " - " + str1.equals(str2));
      // compare str3 = str4 
      System.out.println("Compare " + str3 + " and " + str4 + " - " + str3.equals(str4));
      // compare str4 != str5 
      System.out.println("Compare " + str4 + " and " + str5 + " - " + str4.equals(str5));
      // compare str1 != str4 
      System.out.println("Compare " + str1 + " and " + str4 + " - " + str1.equals(str4));
   }
}



Output: 

Compare HelloWorld and Flower – false
Compare Hello and Hello – true
Comparing Hello and hello – false
Compare HelloWorld and Hello – false


Java String.equalsIgnoreCase() method

This method compares two strings regardless of lowercase or uppercase of string. Returns true if the argument is not null and it represents an equivalent String ignoring case; false otherwise.

public class IgnoreCaseDemo
{
   public static void main(String[] args) 
   {
      String str1 = new String("Helloworld"); 
      String str2 = new String("Flower"); 
      String str3 = new String("Hello"); 
      String str4 = new String("Hello"); 
      String str5 = new String("hello"); 
      // compare for str1 != str2 
      System.out.println(str1.equalsIgnoreCase(str2));
      // compare for str3 = str4 
      System.out.println(str3.equalsIgnoreCase(str4));
      // compare for str4 = str5 
      System.out.println(str4.equalsIgnoreCase(str5));
      // compare for str1 != str4 
      System.out.println(str1.equalsIgnoreCase(str4)); 
   }
}



Output:

false
true
true
false


User defined function

Now let’s define a method which compares value based on condition mentioned below.

Also read – learn strings in java

It returns positive value if str1 > str2, returns negative value if str1 < str2 and returns 0 if str1 == str2. Now let’s see an example,

public class CompareTwoStringsInJava 
{
   public static int strCompare(String strFirst, String strSecond)
   {
      int one = strFirst.length(); 
      int two = strSecond.length(); 
      int minimum = Math.min(one, two);
      for(int a = 0; a < minimum; a++) 
      { 
         int ch1 = (int)strFirst.charAt(a); 
         int ch2 = (int)strSecond.charAt(a);
         if(ch1 != ch2) 
         { 
            return ch1 - ch2; 
         } 
      }
      // case str1 and str2 
      if(one != two) 
      { 
         return one - two; 
      }
      // if none of conditions is true, 
      // for both strings are equal 
      else 
      { 
         return 0; 
      } 
   }

   public static void main(String[] args) 
   {
      String str1 = new String("Helloworld"); 
      String str2 = new String("Flower"); 
      String str3 = new String("Hello"); 
      String str4 = new String("Hello");
      // compare str1 < str2
      System.out.println("Compare " + str1 + " and " + str2 + " : " + strCompare(str1, str2));
      // compare str3 = str4 
      System.out.println("Compare " + str3 + " and " + str4 + " : " + strCompare(str3, str4));
      // compare str1 > str4 
      System.out.println("Compare " + str1 + " and " + str4 + " : " + strCompare(str1, str4));
   }
}



Output:

Java compare two strings

Objects.equals() method

This method returns

  • true if str1 = str2
  • false if str1 != str2
  • true if arguments are null
  • false if one argument is null
import java.util.Objects;

public class ObjectEqualsDemo
{
   public static void main(String[] args) 
   {
      String str1 = new String("Helloworld"); 
      String str2 = new String("Hello"); 
      String str3 = new String("Hello"); 
      String str4 = null; 
      String str5 = null; 
      // compare str1 != str2 
      System.out.println("Compare " + str1 + " and " + str2 + " : " + Objects.equals(str1, str2));
      // compare str2 = str3 
      System.out.println("Compare " + str2 + " and " + str3 + " : " + Objects.equals(str2, str3));
      // compare str1 != str4 
      System.out.println("Compare " + str1 + " and " + str4 + " : " + Objects.equals(str1, str4));
      // compare str4 = str5 
      System.out.println("Compare " + str4 + " and " + str5 + " : " + Objects.equals(str4, str5));
   }
}



Output:

Compare Helloworld and Hello : false
Compare Hello and Hello : true
Compare Helloworld and null : false
Compare null and null : true


Reference – oracle help center