
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java String comparison, differences between ==, equals, matches, compareTo().
When working with strings in Java, one of the most common operations is comparing strings to check for equality or ordering. Java provides several methods to perform string comparison: equals(), matches(), and compareTo(). While all of them are used to compare strings, each method serves a different purpose and behaves differently.
Different approaches
Following are the different approaches for string comparison ?
When to Use Each Method
- Use equals() when you need to check if two strings are identical in content.
- Use matches() when you need to check if a string follows a specific pattern, such as validating formats like email addresses, phone numbers, or any other custom regular expressions.
- Use compareTo() when you need to order or sort strings based on their lexicographical order.
equals() Method
The equals() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Syntax
public boolean equals(Object obj)
Key Points
- It performs a case-sensitive comparison of the string's content.
- It returns true if the strings are exactly the same (case-sensitive).
- It is not null-safe; calling equals() on a null reference will result in a NullPointerException.
Example
public class Sample{ public static void main(String []args){ String s1 = "tutorialspoint"; String s2 = "tutorialspoint"; String s3 = new String ("Tutorials Point"); System.out.println(s1.equals(s2)); System.out.println(s2.equals(s3)); } }
Output
true false
You can also compare two strings using the == operator. But, it compares references of the given variables not values.
matches() Method
The matches() method of the String class tells whether or not this string matches the given regular expression. An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression Pattern.matches(regex, str).
Syntax
public boolean matches(String regex)
Key Points
- It is specifically used for pattern matching.
- The string is checked against the entire regular expression.
- Unlike equals(), it can perform more complex comparisons such as matching specific patterns (e.g., email addresses, phone numbers, etc.).
Example
public class Sample { public static void main(String []args) { String s1 = "tutorialspoint"; String s2 = "tutorialspoint"; String s3 = new String ("Tutorials Point"); System.out.println(s1 == s2); System.out.println(s2 == s3); } }
Output
true false
compareTo() Method
The compareTo() method is used to compare two strings lexicographically. This method returns an integer value:
- 0 if the strings are equal,
- A negative integer if the first string is lexicographically less than the second string,
- A positive integer if the first string is lexicographically greater than the second string.
Syntax
public int compareTo(String anotherString)
Key Points
- It compares the strings based on their Unicode values.
- It is case-sensitive.
- It is useful when you need to order strings (e.g., sorting).
Example
import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :" ); System.out.println(Str.matches("(.*)Tutorials(.*)")); System.out.print("Return Value :" ); System.out.println(Str.matches("Tutorials")); System.out.print("Return Value :" ); System.out.println(Str.matches("Welcome(.*)")); } }
Output
Return Value :true Return Value :false Return Value :true
Key Differences Between equals(), matches(), and compareTo()
Below is the comparison table to show the key differences between equals(), matches(), and compareTo() ?
Method |
Purpose |
Comparison Type |
Case Sensitivity |
Use Case |
equals() |
Check if two strings are the same |
Exact equality |
Case-sensitive |
Comparing two strings for equality. |
matches() |
Checks if the string matches a regular expression |
Pattern matching (regex) |
Case-sensitive |
Validating strings against patterns (e.g., email) |
compareTo() |
Compares two strings lexicographically |
Lexicographical order |
Case-sensitive |
Ordering strings or sorting. |