Explore Java Hacks and Tutorials
Be Social. Be Updated.

Compare two strings in Java lexicographically

Compare two strings lexicographically
The compareTo(T ob) method in the Comparable class is implemented in the String class and this implemented method takes java.lang.String as parameter. This method compares two strings lexicographically. The term lexicographically might seem new and weird. Whatever, don't get worried. I'll explain this method in a very easy way.

The compareTo method in the String class compares two strings. Here two sample cases,

1. When both of the strings differ in characters at a particular position then characters of both the strings are checked in order whether they match are not i.e. the 'i'th character of one string is checked with the 'i'th character of another string for equality (whether they are equal or not) starting from i=0 till the end of the smallest string (provided in both the strings there is 'i'th character

The above statement here is returned where st is the object in which compareTo() is called and st1 is the parameter for compareTo(). You'll have to note that though the Strings differ in length, the length property is not given the first preference i.e. the difference in the ascii values of the characters is returned but not the difference in lengths.

2. When both of the strings does not differ in characters at same positions, the difference of their lengths is returned. The length of the String object which is passed to the compareTo() method is subtracted from the length of the String object in which this method is called. Here, right from the starting of the string every character of the string is checked whether it matches or not. In this case, every character is matched and only the lengths differ. Consider an example of two such strings for a better understand.


Here the first string is a part of the second string and the second string contains the character e in addition.

When we consider the string is a set of characters in order, then first string is a subset of the second string. So for the compareTo(String) method to return difference in lengths, one string should be subset of the other.

Here when the compareTo method is called from the first string passing the second string as parameter, then -1 is returned i.e. length of first string - length of second string.

class CompareToExample
    public static void main(String args[])
    String st="abcde";
    String st1="abcdf";




Here compareTo() method is called from the st and st1 is sent as parameter.
The first 4 characters of both the strings are identical but the last character differ as a result, ascii of value of e - ascii value of f is returned. As e and f have consecutive ascii values and e being smaller than f in terms of ascii value, e-f gives rise to -1. If reverse is the case, however 1 is returned (for example).

Simple. Isn't it. If you still have a doubt on the compareTo(String) method feel free to drop a comment. I'll respond as soon as possible.

Posted by at on

Tags: java.lang String Handling