Java compare strings

Hey guys!! Welcome to flower brackets blog. Today in this post we are going to learn java compare strings.

java compare strings

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

  • compareTo() method
  • equals() method
  • == operator

Let us understand each method with an example,

using method : this method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.

Also read – learn strings in java

If result is negative string object lexicographically precedes the argument string. If result is positive string object lexicographically follows the argument string and if result is equal compareTo returns 0 exactly when the equals(Object) method would return true.

Now let’s see an example,

public class FB 
{
   public static int strCompare(String first, String second) 
   {
      int len1 = first.length(); 
      int len2 = second.length(); 
      for(int a = 0; a < len1 && a < len2; a++) 
      { 
         int strOne = (int)first.charAt(a); 
         int strTwo = (int)second.charAt(a);
         if(strOne == strTwo) 
         { 
            continue; 
         }
         else 
         { 
            return strOne - strTwo; 
         }
      }
      if(len1 < len2) 
      { 
         return len1 - len2; 
      }
      else if(len1 > len2) 
      { 
         return len1 - len2; 
      }
      else 
      { 
         return 0; 
      } 
   }

   public static void main(String[] args) 
   {
      String string1 = new String("helloworld"); 
      String string2 = new String("flower"); 
      String string3 = new String("hello"); 
      String string4 = new String("hello"); 
      // compare for string 1 < string 2 
      System.out.println("String 1 < 2 : " + strCompare(string1, string2));
      // compare for string 3 = string 4 
      System.out.println("String 3 = 4 : " + strCompare(string3, string4));
      // compare for string 1 > string 4 
      System.out.println("String 1 > 4 : " + strCompare(string1, string4));
   }
}

Output:  

String 1 < 2 : 2
String 3 = 4 : 0
String 1 > 4 : 5


Java string equals()

Equals method returns true if the arguments are equal to each other and false otherwise.

Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned.

Otherwise, equality is determined by using the equals method of the first argument.

  • public boolean equalsIgnoreCase(String another) – this method compares one string to another string ignoring case.
  • public boolean equals(Object another) – this method compares given string to the specified object.

Let’s see an example,

public class EqualsJavaDemo
{
   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.equals(str2));
      // compare for str3 = str4 
      System.out.println(str3.equals(str4));
      // compare for str4 != str5 
      System.out.println(str4.equals(str5));
      // compare for str1 != str4 
      System.out.println(str1.equals(str4));
   }
}

Output: 

false
true
false
false

// compares string to another string, ignoring case.
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


Objects.equals() method

This method returns true if the arguments are equal to each other and false otherwise. Meanwhile, if both arguments are null, true is returned and if one argument is null, false is returned.

Otherwise, equality is determined by using the equals method of the first argument.

import java.util.Objects;

public class EqualsDemo 
{
   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 for str1 != str2 
      System.out.println(Objects.equals(str1, str2));
      // compare for str2 = str3 
      System.out.println(Objects.equals(str2, str3));
      // compare for str1 != str4 
      System.out.println(Objects.equals(str1, str4));
      // compare for str4 = str5
      System.out.println(Objects.equals(str4, str5));
   }
}

Output:

false
true
false
true


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

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 for str1 < str2 
      System.out.println(str1.compareTo(str2));
      // compare for str3 = str4
      System.out.println(str3.compareTo(str4));
      // compare for str1 > str4
      System.out.println(str1.compareTo(str4));
   }
}

Output:

2
0
5


Reference – oracle help center.

Related Posts