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

Java compare strings

In java there are three methods to compare a string. Namely,

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

Let us understand each method with an example,

Also Read – Learn Strings In Java

compareTo() : String compareTo() method is used in sorting. compareTo() method compares value in alphabetical order and returns value that relate if first string is equal to second string or greater than or lesser than second string.

public class StringCompareJava
{
   public static void main(String[] args)
   {
      String strFirst = "Flower";
      String strSecond = "Flower";
      String strThird = "Brackets";
 
      System.out.println("Java compare strings : ");

      // here output will be zero since strFirst == strSecond
      System.out.println(strFirst.compareTo(strSecond));
 
      // here output will be positive value since strFirst > strThird
      System.out.println(strFirst.compareTo(strThird));
 
      // here output will be negative value since strThird < strFirst
      System.out.println(strThird.compareTo(strFirst));
   }
}

Output:  

Java compare strings :

0 4 -4


compareToIgnoreCase() : this method is same as above method. In this, method ignores case.

public class IgnoreCase 
{
   public static void main(String[] args) 
   {
      String strOne = "Flower";
      String strTwo = "brackets";
      String strThree = "BRACKETS";

      System.out.println("String comparison java : " + trOne.compareToIgnoreCase(strTwo)); 
      System.out.println("String comparison java : " + strTwo.compareToIgnoreCase(strOne));
      System.out.println("String comparison java : " + strThree.compareToIgnoreCase(strTwo)); 
   }
}

Output:

String comparison java : 4
String comparison java : -4
String comparison java : 0


equals() : equals() method basically compares value of a string for equality. There are two methods,

  • public boolean equalsIgnoreCase(another string) – this method compares one string to another string ignoring smaller case and bigger case.
  • public boolean equals(another object) – this method compares given string to the specified object.
public class StringCompareJava
{
   public static void main(String[] args)
   {
      String strFirst = "Flower";
      String strSecond = "Flower";
      String strThird = new String("Flower");
      String strFourth = "Brackets";
      String strFifth = "FLOWER"; 

      System.out.println("Compare strings java : ");
 
      System.out.println(strFirst.equals(strSecond));
 
      System.out.println(strFirst.equals(strThird));
 
      System.out.println(strFirst.equals(strFourth));
 
      System.out.println(strFirst.equals(strFifth));

      System.out.println(strFirst.equalsIgnoreCase(strThird));
   }
}

Output: 

Compare strings java :

true
true
false
false
true


equalsAny() : This method returns true if any of the given strings match against the first String case sensitively else false is returned.

Also Read – Learn String Literal In Java

In this method first argument is a String and second argument is multi arguments type CharSequence.

equalsAnyIgnoreCase() : This method is same as above except ignores casing.


== operator method : == operator method basically compares references(not value).

public class StringCompareJava
{
   public static void main(String[] args)
   {
      String strFirst = "Flower";
      String strSecond = "Flower";
      String strThird = new String("Flower");
 
      // here output will be true since strFirst and strSecond refer to same instance
      
      System.out.println("Comparing strings in java : ");
      System.out.println(strFirst == strSecond);
 
      // here output will be false since strThird refer to instance created
      System.out.println(strFirst == strThird);
   }
}

Output:

how to compare strings in java


Java compare strings – using Object class

In java Object is a utility class in which we have static equals() method to compare strings. This method first compares string using their address, that is, ” == ” .

Also Read – Sort String Array Java

Returns true if two strings are equal. Meanwhile if both arguments are null it returns true, else returns false if one argument is null.

String class equals() method is case sensitive because it internally calls string class equals method.

import java.util.Objects;

public class EqualJava 
{
   public static void main(String[] args) 
   {
      String str1 = "hello world";
      String str2 = "hello world";
      String str3 = new String("hello world");

      System.out.println("Java check if two strings are equal : " + Objects.equals(str1, str2));
      System.out.println("Java check if two strings are equal : " + Objects.equals(str1, str3));

      System.out.println("Java check if two strings are equal : " + Objects.equals(null, null));
      System.out.println("Java check if two strings are equal : " + Objects.equals(null, str1));
   }
}

Output:

Java check if two strings are equal : true
Java check if two strings are equal : true
Java check if two strings are equal : true
Java check if two strings are equal : false


conclusion

That’s it guys. This was all about java compare strings. I hope you have understood how to compare strings in java.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like