Java is a programming language that has a wide variety of methods and functions to perform specific tasks. Strings in Java are considered one of the fundamental concepts in programming. Java offers several built-in string methods to manipulate the string/textual data. One such method is the compareTo() method which compares the two strings and returns the value accordingly. Each character is first converted into its Unicode and then compared.
In this write-up, we will elaborate in detail the working of compareTo() method of the String class in Java.
Working of String compareTo() Method in Java
The String compareTo() method implements the Comparable interface of Java that compares one string to another string lexicographically. The term lexicographically primarily means that the elements are sorted on the basis of alphabet, numbers, or dates. In simple words lexicographic is also referred to as dictionary order. Let us understand the pattern of the compareTo() method with an example.
Let us suppose we have two strings X and Y and they return:
- A negative value if X<Y.
- A positive value if X>Y.
- If X and Y are equal, the value is 0.
The syntax of the compareTo() method is depicted below:
public int compareTo(firstString secondString)
Example 1: Implementation of compareTo() Method in Java
The compareTo() method in the code below will compare the strings with each other and return the value accordingly:
public class Comparison{
//Declare the Main Class.
public static void main(String args[]){
//Declare the strings for comparison
String str1="hi";
String str2="hellotoyou";
String str3="meetyou";
String str4="her";
String str5="nothing";
//Compare str1 with str2.
System.out.println("The comparison between Str1 and Str2 gives:" +str1.compareTo(str2));
//Compare str1 to str3
System.out.println("The comparison between Str1 and Str3 gives:" +str1.compareTo(str3));
//Compare str1 to str4.
System.out.println("The comparison between Str1 and Str4 gives:" +str1.compareTo(str4));
//Compare str1 to str5.
System.out.println("The comparison between Str1 and Str5 gives:" +str1.compareTo(str5));
}
}
In the above piece of code:
- Five strings from str1 to str5 are declared in the Main class.
- The compareTo() method compares string 1 with each of the available strings up to string 5.
- The comparison results of the compareTo() method are shown using the println() method.
Output
The output below provides a comparison in a way that when string 1 and string 2 are compared, the output becomes 4 since character i is 4 times greater than character e. Whereas when string 1 and string 3 are compared the output becomes -5 since h is less than m.

Example 2: Using compareTo() Function With Empty String
If one of the strings is empty when the comparison occurs, the value is returned for the string that contains the characters.
public class Comparison{
//Declare the Main Class.
public static void main(String args[]){
//Declare the strings for comparison
String str1="hi";
String str2="hellotoyou";
String str3="";
System.out.println("CASE OF EMPTY STRING");
//Compare str1 with str2.
System.out.println("The comparison between Str1 and Str2 gives:" +str1.compareTo(str2));
//Compare str1 to str3
System.out.println("The comparison between Str1 and Str3 gives:" +str1.compareTo(str3));
}
}
The above code is the same as the previous one except for the difference that string 3 is kept empty.
Output
The output below returns the value as 2 when compared with an empty string named “str3”. Since the string hi contains two characters, so, two will be printed on the screen.

Example 3: Using compareTo() Function With Case-Sensitive String
Since Java is a case-sensitive programming language, therefore, it treats the lowercase and uppercase strings differently. The two strings are considered different in the below code.
public class Comparison{
//Declare the Main Class.
public static void main(String args[]){
//Declare the strings for comparison
String str1="hi";
String str2="HI";
System.out.println("CASE SENSITIVE STRING");
//Compare str1 with str2.
System.out.println("The comparison between Str1 and Str2 gives:" +str1.compareTo(str2));
}
}
In the above code:
- The first string is declared in lowercase.
- The second string is declared in uppercase.
- The compareTo() method compares the two strings lexicographically.
- The println() method yields the results.
Output
In the output below the two strings yield a difference in value since Java considers uppercase and lowercase different:

Example 4: Using compareTo() Function With Different Classes
The below code depicts a condition if two different classes are declared for the comparison of the two strings using the compareTo() method.
public class classexception {
public static void main(String[] args) {
//It contains a different class
String first = new String("Hello");
//It contains a different class that is StringBuffer.
StringBuffer second = new StringBuffer("Hello to Everyone");
// Print statement for a class exception.
System.out.println(first.compareTo(second));
System.out.println(first.compareTo(second.toString()));
}
}
In the above Java code:
- Two different classes are declared in the Main class.
- When the strings in the two classes are compared, the results occur in the exception.
Output
The output below shows that when the strings of two different classes are compared, an error occurs.

Example 5: Using compareTo() Function With Null Value
The NullPointerException occurs when the compareTo() method gets invoked by the null object.
public class nullexception {
public static void main(String[] args) {
//It consists of null value
String first = null;
// Print statement for null pointer exception
int s = first.compareTo("Hello to the World!");
System.out.println(s);
}
}
In the above code, the string is declared with a null value. The compareTo() method compares the given strings and encounters an error stating that the “cannot invoke null object”.
Output
The output below represents that a null pointer exception is raised when a null value is compared with a string using the compareTo() function.

This sums up the implementation of the compareTo() method in Java.
Conclusion
The comparable interface implements the compareTo() method of the String class in Java. This method works in such a manner that it compares the strings with each other in a lexicographic manner and produces the output accordingly. In this write-up, we have demonstrated the implementation of the compareTo() method of Java in detail along with its conditions.