Java String CompareTo

“The compareTo() is a method that is supported by the java string class for string comparison. This method compares the string lexicographically, which means in alphabetic order. The comparison of the string depends on the Unicode value assigned to each string character. The return value of the compareTo() method is the integer value that can be positive, negative, or zero. The positive integer is obtained when the first-string characters are higher characters than the second-string characters. The negative integer is returned when the first string is lower than the second string using alphabetic order, and it returns zero if they are lexically equal.”

Example 1

We are using the compareTo() method of Java in this particular example to demonstrate how this method compares the string in alphabetical order.

We have a main program inside the class “CompareToCase1”. Here, we have declared three variables which are titled “s1”, “s2,” and “s3”. The variables contain a string of words where “s1” and “s2” have the same string. The “s3” is initialized with a different string. Then, we invoked the compareTo() method, where we passed the “s2” and “s3” variables as an argument. The compareTo method compares these strings with the “s1” string and prints the results.

We have a “0” integer value obtained from the compareTo method as the string “s1,” and “s2” have the same content. On the other hand, we have got the negative value “-7” from the comparison of “s1” and “s3” because they are not the same, and “s3” comes after the “s1” according to alphabetical or dictionary order.

Example 2

The compareTo method of java is also case-sensitive while comparing the strings. We have an example program that determines the case sensitivity of the compareTo method.

We have established the class “CompareToCase2” for determining the string compareTo() method case sensitivity. We have assigned the string in uppercase to the variable “MyString”. Then, the string.out.println method is deployed for the compareTo method. The compareTo method compares the specified string with the string passed as an argument in lowercase. In the next print statement, we have input the uppercase string in the compareToIgnoreCase() method. The compareToIgnoreCase method helps us to ignore the case sensitivity of the comparison strings in the output.

The string is compared with the string of the compareTo method and generates the negative integer “-32” result. Hence, the compareTo method is a case-sensitive method. But when we utilize the compareToIgnoreCase, the resultant value is a positive integer which means the case is ignored.

Example 3

Now, we are checking the equivalence of the strings by using the compareTo method. For this, we have to use the conditional method in our program.

We have the main method declaration inside the “CompareToCase3” class. We have created two strings there which are defined as “MyStr1” and “MyStr2”. We have stored different strings in both variables. The strings can be displayed by the print method of java on the console. Then, we employed the if-else block. We have an “if” condition where we have provided the compareTo method. The “MyStr2” is used as a parameter inside the compareTo method and compared with the string “MyStr1”. If these strings are matched with each other, the results are “0,” and the “if” block statement will be executed. Otherwise, we have else block execution.

The string comparison results displayed the statement that strings are not equal on the console below, as we have provided the different strings.

Example 4

Although the compareTo method of java performs the same for the comparison of the string that has a different length, let’s consider this statement for the below program.

The code snippet has the “CompareToCase4” class, where we have implemented the program’s main method. Here, we have utilized the print method of java and passed the string of the character “y,” which is compared with the string inside the compareTo method. The character string remains the same, but we changed the length of the string by providing the new strings containing the same character “y”. Next, we compared the integer string with the integer string of different lengths specified in the compareTo method. The results will be printed upon execution of each print statement using the compareTo method.

The different outcomes of the varied string lengths are displayed below.

Example 5

The compareTo method also generates an error “NullPointerException” by the compiler. This error is thrown when the compareTo method takes the null value as an argument.

We have a “CompareToCase5” class which drives the main program. Within the java main block, we have defined the “Str_Val1” variable, which holds the string. Then, we have defined another “Str_Val2” variable, which holds the “null” value. The strings are called for the comparison within the “system.out.println” method. The compareTo method is also employed there. We have compared the “Str_Val1” with the “Str_Val2”. The execution results will be shown on the console.

When these strings are compared, it raises an exception of “NullPointerException” output as the string used as a parameter inside the compareTo method has a null value.

Example 6

Moreover, we have another exception case regarding the string compareTo method. The ClassCastException is raised when we have inconsistent types of objects for comparison.

We have built a class as “Employees” where we have defined the “emp_Name” object and also called the constructor for “Employee” of the class. The “emp_name” is defined with the “StringIs” object called in the constructor. Then, we have the main method inside the “CompareToCase6” class. We have specified some employees’ names by calling the class “Employees”. We have added the employee’s name with the aid of the add method. After that, we have a binary search operation for a comparison of the employee names string with another string.

The execution of the above program meets except the ClassCastException, as the provided string is incompatible.


The compareTo method is used for comparing the string by following the dictionary order and returning the expected outputs. We have seen many scenarios with the example program where the compareTo method performs differently. The case-sensitive scenario is also implemented for the compareTo method, and how to ignore the case-sensitive cases is also explained. Furthermore, we have the compareTo() method exception cases which we can avoid while utilizing the compareTo() method in java.