How do I compare strings in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Comparing strings in Java is a fundamental task in many Java applications whether you're sorting data, validating inputs, or carrying out any operation that requires comparison of two or more strings. Due to the nature of strings in Java, there are several methods to accomplish this, each with its own use case and nuances.
Understanding Java Strings
In Java, strings are objects managed by the String class. This setup is crucial because it means that strings are not just mere sequences of characters as seen in some other programming languages, but instances of a class with methods and properties.
Ways to Compare Strings in Java
1. Using the equals() Method
The equals() method checks if two strings have the exact same sequence of characters:
This method is case-sensitive and does not consider string case while comparing.
2. Using the equalsIgnoreCase() Method
Java provides the equalsIgnoreCase() method to compare two strings regardless of their case:
This method is very useful in scenarios where you want to perform comparisons devoid of case sensitivity, like user input validations.
3. Using the compareTo() Method
The compareTo() method compares two strings lexicographically and returns an integer:
- A result
< 0indicates that str1 is lexicographically less than str2. - A result
= 0indicates that both strings are equal. - A result
> 0indicates str1 is greater.
4. Using the compareToIgnoreCase() Method
Similar to compareTo(), but ignoring character case:
Special Cases When Comparing Strings
- Null Strings: Comparing strings where one or both strings are
nullcan lead toNullPointerException. Safe comparison necessitatesnullchecks. - Pool of Interned Strings: The Java String Pool — a storage area in Java heap — holds literal strings. Because of String interning, two strings having the same characters can point to the same memory location if they are interned.
- String concatenation: Keep in mind that modifying one string which another is referenced to, doesn't affect the other string due to string immutability.
Best Practices
- Use
equals()andequalsIgnoreCase()for comparing equality. - Use
compareTo()andcompareToIgnoreCase()for sorting or when lexical ordering is required. - Always check for
nullto avoidNullPointerException.
Summary Table
| Method | Case Sensitive | Return Type | Use Case |
equals(Object anObject) | Yes | boolean | Check if two strings are exactly the same. |
equalsIgnoreCase(String anotherString) | No | boolean | Check equality ignoring case differences. |
compareTo(String anotherString) | Yes | int (negative, zero, positive) | Lexicographically compare two strings. |
compareToIgnoreCase(String str) | No | int (negative, zero, positive) | Lexical comparison ignoring case. |
Using the right string comparison method not only ensures correctness in your Java application but also enhances security and performance, especially in data-sensitive and high-load applications.
Related reading
- How do I configure HikariCP in my Spring Boot app in my application.properties files?
- How do I configure spring-kafka to ignore messages in the wrong format?
- How do I convert a byte array to Base64 in Java?
- How do I convert a Java 8 IntStream to a List?
- How do I convert a Map to List in Java?
- How do I convert a String to an InputStream in Java?
- How do I convert an OutputStream to an InputStream?
- How do I convert CamelCase into human-readable names in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.