dineshonjava

How to Compare two Strings in Java

Compare two Strings in Java
In Java application string comparison is very common requirement in the all real world scenarios. So it is very basic need to know how to compare two string values each other. Although Java provide different ways to compare two string each other in an application. String is immutable in nature and so its immutability nature has lots of benefits like Security, String Pool, and Performance.

In this tutorial of string comparison we will describe different ways to compare two String in Java. There are two methods equals() or compareTo() in Java compare two string values to each other.


There are following examples of string comparison in java. Let's see below String comparison:

  • String comparison using equals method
  • String comparison using equalsIgnoreCase method
  • String comparison using compareTo method
  • String comparison using compareToIgnoreCase method


String comparison using equals method
As we all know that equals() method of object class is using for compare two objects for semantic comparison. String class overrides equals() method of object class for content equality. So if two strings contain same characters, in same order and in same case they will be equals by equals() method. Read more about equals() method in java from this article.

For example
We will compare two strings "Java" and "Java" by equals() method, it will return true but for "Java" and "JAVA" it will return false.

public static void main(String[] args) {
   System.out.println("Java".equals("Java"));
   System.out.println("Java".equals("JAVA"));
  }

String comparison using equalsIgnoreCase method
In java equalsIgnoreCase() method is same as equals() method but it is more liberal than equals and it compare two strings ignoring their case. That is if two strings have same characters and in same order despite of their case then equalsIgnoreCase() method will always return true. equalsIgnoreCase() method compare two strings by case insensitive i.e strings are either in lower case, upper case, Capital case or Camel case they will be equal by equalsIgnoreCase.

For example
There were two strings of earlier example "Java" and "JAVA". Now we compare these strings by equalsIgnoreCase() method and it will return true but "Java" and "Dinesh on Java" will not be same and it will return false because they don't contain same characters.

public static void main(String[] args) {
   System.out.println("Java".equalsIgnoreCase("Java"));
   System.out.println("Java".equalsIgnoreCase("JAVA"));
   System.out.println("Java".equalsIgnoreCase("Dinesh on Java"));
  }


String comparison using compareTo method
In java String class overrides compareTo() method of Compareable interface. compareTo() method compares two strings in different manner unlike equals() and equalsIgnoreCase(), it actually compares two string lexicographically. This means if two strings lexicographically equal, precedes or follows each other. compareTo() method is commonly used for sorting of strings lexicographically . This is also called natural order of String. compareTo() method return int value if it returns zero if two strings are same, if it returns less than zero if calling string comes before argument string and if it returns greater than zero if calling string comes later than argument string.

For example:


  • There are two string "Java" and "Java". If we will compare these strings by compareTo() method it will return 0.
  • There are two string "CPP" and "Java". If we will compare these strings by compareTo() method it will return -7.
  • There are two string "Java" and "CPP". If we will compare these strings by compareTo() method it will return 7.


public static void main(String[] args) {
   System.out.println("Java".compareTo("Java"));
   System.out.println("CPP".compareTo("Java"));
   System.out.println("Java".compareTo("CPP"));
}

String comparison using compareToIgnoreCase method

compareToIgnoreCase() method is similar to compareTo() method with ignoring case like equalsIgnoreCase() and return same values as returned by compareTo during String comparison.

Summary
We have seen multiple ways to compare two strings each other. Now it depends on your requirement which is suitable for you. If you want to compare two strings with case sensitive then go with equals() method if case is not matter in comparison of two strings then compare with equalsIgnoreCase() method. If want to compare two strings lexicographically equal then use compareTo() method.