Core JAVA

How to 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.

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago