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 two strings in java.

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.

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


Comparing String 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 learn how to compare characters in a string in java.

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


String comparison 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


String comparison 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.

Also read – difference between equal “==” operator and equals() method in java

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.

Now let’s learn how to compare characters in a string in java.

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

String comparison 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


how to compare two strings in java without using equals

Let’s learn how to compare two strings in java without using equals. Here’s java program without using string method equals().

public class CompareWithoutEquals 
{
   public static boolean compare(String strOne, String strTwo)
   {
      if(strOne == null || strTwo == null)
      {
         return false;
      }
      // comparing lengths
      if(strOne.length() != strTwo.length())
      {
         return false;
      }
      // comparing all characters
      for(int a = 0; a < strOne.length() ; a++) 
      {
         if(strOne.charAt(a) != strTwo.charAt(a))
         {
            return false;
         }
      }
      // return if both strings are equal
      return true;
   }
   public static void main(String[] args) 
   {
      String str1 = "helloworld";
      String str2 = "helloworld";
      System.out.println(compare(str1, str2));
      str1 = "helloworld";
      str2 = "helloworld ";
      System.out.println(compare(str1, str2));
      str1 = "helloworld";
      str2 = " ";
      System.out.println(compare(str1, str2));
   }
}


Output:

true
false
false


java compare strings alphabetically

Now let’s learn java compare strings alphabetically. Comparing strings by their alphabetical order can be done using String.compareTo() method.

compareTo() method returns following values,

  • if (str1 > string2) returns +ve value.
  • if (str1 == str2) returns 0.
  • if (str1 < str2) returns -ve value.

Here’s the java program.

public class CompareAlphabetically 
{
   public static void main(String[] args) 
   {
      String str1 = "Radha"; 
      String str2 = "Radha"; 
      String str3 = "Shrikrishna"; 
      String str4 = "ABC"; 
      System.out.println("java compare strings alphabetically with compareto: ");
      System.out.println(str1.compareTo(str2)); 
      System.out.println(str1.compareTo(str3)); 
      System.out.println(str1.compareTo(str4));
   }
}


Output:

java compare strings alphabetically with compareto:
0
-1
17