Java compare two strings

Let’s learn java compare two strings.

Java compare two strings

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

NOTE: Do not use == operator for comparing Strings

Because basically equal() method and == operator compare objects to check equality. Here “==” operator is used for reference comparison and .equals() method for content comparison.

== operator in java checks if objects point to same memory location. While .equals() method compares both object values is equal or not.

// java string compare
public class EqualOperatorDemo
{
   public static void main(String[] args)
   {
      String str1 = new String("helloworld");
      String str2 = new String("helloworld");
      System.out.println(str1 == str2);
      System.out.println(str1.equals(str2));
   }
}


Output:

false
true


using compareTo() method:

compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

Here str1 and str2 represent two strings. compareTo() method compares two strings and if,

  • str1 > str2 returns positive value
  • str1 < str2 returns negative value
  • str1 == str2 returns 0

Let’s see an example on java string compare.

// java string compare
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


using String.equals() method in java

String.equals() method compares two strings based on value. 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


using String.equalsIgnoreCase() method in java

String.equalsIgnoreCase() 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.

It returns positive value if str1 > str2, returns negative value if str1 < str2 and returns 0 if str1 == str2.

In the below example we are using charAt() method to compare characters in a string in java.

charAt() method returns the char value at the specified index of this string. The first char value is at index 0. Here’s 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

using Objects.equals() method

Objects.equals() 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