In Java, comparing two strings is the most common task in situations like file validation, user authentication, form input processing, and database queries. The compareTo() method helps compare two strings based on their lexicographical order (dictionary order).
In this blog, we will explore how to use the compareTo() method with examples.
Table of Contents:
- What is String.compareTo() Method in Java?
- Variants of CompareTo() Method
- Examples to Compare Strings using String.compareTo() Method in Java
- Exceptions in Java String compareTo() Method
- Conclusion
What is String.compareTo() Method in Java?
The compareTo() method in Java is used to compare two strings lexicographically (dictionary order). It compares the ASCII values of characters in both strings. This method is case-sensitive, meaning uppercase and lowercase letters are treated differently.
Syntax:
int result = string1.compareTo(string2);
Parameters:
- string1 is the first string to compare
- string2 is the second string to compare
- result is an integer either( 0, positive or negative).
Return Type: It returns an integer value
- Returns 0: If both strings are equal.
- Negative value: If string1 is lexicographically less than string2.
- Positive value: If string1 is lexicographically greater than string2.
Variants of CompareTo() Method
Here are the following three variants of the compareTo() method in Java:
- Using int compareTo(Object obj)
- Using int compareTo(String AnotherString)
- Using int compareToIgnoreCase(String str)
1. Using int compareTo(Object obj)
This method is used to compare a String with another Object. However, it is not commonly used because it requires explicit typecasting.
Syntax:
int result = string1.compareTo(object);
Parameters:
- object: The object to be compared.
Example:
Output:
Explanation: In the above code, we have compared a string and an object with the help of compareTo() method.
2. Using int compareTo(String anotherString)
This method compares two strings lexicographically based on their ASCII values.
Syntax:
int result = string1.compareTo(string2);
Parameters: string2 is the string to be compared.
Example:
Output:
Explanation: In the above code, both the strings are not equal, the difference between ‘I’ and ’i’ is -32. So it returns -32.
3. Using int compareToIgnoreCase(String str)
This method is similar to compareTo(), but it ignores case differences while comparing.
Syntax:
int result = string1.compareToIgnoreCase(anotherString);
Example:
Output:
Explanation: In the above code, even though one string is in lowercase and the other is in uppercase, compareToIgnoreCase() treats them as equal and returns 0.
Examples to Compare Strings using String.compareTo() Method in Java
Here are the examples to compare strings using String.compareTo() Method in Java:
Example1: Comparing Strings
Output:
Explanation: In the above code,
1. str1.compareTo(str2): “Apple”.compareTo(“Banana”)
- The method compares characters lexicographically.
- ‘A’ (ASCII: 65) comes before ‘B’ (ASCII: 66).
- Since “Apple” is less than “Banana”, the result is negative (-1 or another negative value).
2. str1.compareTo(str3): “Apple”.compareTo(“Apple”)
- Both strings are identical.
- The result is 0, which means both the strings are equal.
3. str2.compareTo(str1): “Banana”.compareTo(“Apple”)
- ‘B’ (ASCII: 66) comes after ‘A’ (ASCII: 65).
- Since “Banana” is greater than “Apple“, the result is positive (1 or another positive value).
Example 2: Check if Two Strings are Equal
In the following example, we will check if two strings are equal or not using String.compareTo() method:
Example:
Output:
Explanation: The compareTo() method compares “Learn JavaScript” and “Learn Java” lexicographically. Since “Learn JavaScript” is longer and comes after “Learn Java“, compareTo() returns a positive value, so the output will be “str1 and str2 are not equal“.
Example 3: compareTo() With Case
The compareTo() method in Java is case-sensitive, meaning it considers uppercase and lowercase letters as different based on their ASCII values.
Example:
Output:
Explanation: This program compares two strings, “Java” and “java“, using the compareTo() method. Since uppercase ‘J’ has a smaller ASCII value than lowercase ‘j’, the method returns -32, meaning “Java” comes before “java” in dictionary order.
Exceptions in Java String compareTo() Method
Below are the possible exceptions that may occur while using compareTo() method in Java:
1. NullPointerException
The compareTo() method throws a NullPointerException if the string is compared to null.
Example:
Output:
Explanation: Since str2 is null, calling compareTo() on str1 leads to a NullPointerException.
2. compareTo() ClassCastException
The compareTo() method can throw a ClassCastException when the object is not a String.
Example:
Output:
Explanation: Here, str2 is an Integer, so explicitly changing it to String will lead to ClassCastException.
Conclusion
In this blog, we have learned how to compare strings using the compareTo() method in Java. We also explored its different versions with examples and also explored possible exceptions that can occur while using this method.
If you want to learn more about Java, you may refer to our Java Course.