In Java, the java.lang.String class provides the compareTo() method to compare two strings lexicographically (in dictionary order). This comparison is based on the Unicode value of characters present in the string.
The String class provides the following two methods to compare String:
| Method | Description |
|---|---|
| compareTo(String anotherString) | The method compares two strings lexicographically. |
| compareToIgnoreCase(String str) | The method compares two strings lexicographically, ignoring case differences. |
The method to compare two strings lexicographically. It returns 0 exactly when the String.equals(Object) method would return true.
public int compareTo(String anotherString)
Parameters: The method accepts a string to be compared as a parameter.
Output:
Comparing str1 and str2: -1 Comparing str2 and str3: 1 Comparing str1 and str3: 0
Explanation
In this example, "Apple" comes before "Banana", so str1.compareTo(str2) gives a negative value. "Apple" and "Apple" are equal, so their comparison gives 0.
Output:
Difference of the string 'TpointTech' and 'tpointTech' is: -32
Explanation
In the above program, the first string is “TpointTech” and the second string is “tpointTech”. When we compare both the strings each other, a mismatch occurs. The Unicode value of the character ‘T’ and ‘t’ is 84 and 116, respectively. Their difference is -32 (84 – 116 = -32). So, the output is -32.
For ignoring the case sensitivity of characters, the String class provides the String.compareToIgnoreCase() method.
This method compares two strings in dictionary order (lexicographically) by ignoring case sensitivity. The method first converts all the characters of the string in lowercase, and then compares the strings.
Parameters: The method accepts string to be compared as a parameter.
Returns: The method returns a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.
Output:
Comparing str1 and str2: -1 Comparing str2 and str3: 1 Comparing str1 and str3: 0
Explanation
The String.compareToIgnoreCase() method compares two strings lexicographically without considering uppercase and lowercase differences.
For example, "Apple" and "APPLE" are considered equal, so their comparison returns 0.
The String.compareTo() method may raise the following two exceptions:
To read more Exception Handling in Java
ClassCastException is a runtime exception that occurs only when objects of incompatible types are compared using the String.compareTo() method.
Example: Comparing Two Different Objects
Output:
Runtime Error: Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap') at Main.main(Main.java:6)
The NullPointerException is raised if either of the objects that are getting compared is null. It makes sure that one must explicitly take care of the null values and stop this unexpected behaviour.
Example: Comparing String with Null Object
Output:
Runtime Error: Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:6)
We request you to subscribe our newsletter for upcoming updates.