Java Compare Two Strings Lexicographically | String compareTo method works internally


How to compare two string in java Lexicographically, Lexicographically meaning in java, How CompareTo method works internally, Example with Step by step explanation.

Today we will learn about comparing two strings lexicographically in Java. Many of you might be aware about how to compare two strings in java but not in a manner of lexicographically. Lexicographically word looks somewhat new and weird. Do not worry, we will go step by step easy understandable way and how it works internally.

First, We will go through example of how to compare two string values are same/equal or not.

Compare Two Strings Lexicographicall

String equals example:

package blog.java.w3schools.string.compare.lexicographically;

public class StringCompare {
 public static void main(String[] args) {
  String stringOne = "w3schools";
  String stringTwo = "java";
  String stringThree = "w3schools";

  System.out.println("Comparing stringOne and stringTwo : " + stringOne.equals(stringTwo));
  System.out.println("Comparing stringOne and stringThree : " + stringOne.equals(stringThree));

Comparing stringOne and stringTwo : false
Comparing stringOne and stringThree : true

String class in java has a method called equals which takes input parameter Object type. It compares the contents of two string are same. It returns true if and only if the passed value is not null value and contents are same, else false.

In our example program, 

stringOne and stringTwo values are different. So stringOne.equals(stringTwo) returns false.
stringOne and stringThree values are same. So stringOne.equals(stringThree) returns true.

Comparing two string lexicographically:

Comparing two string lexicographically is done by calling compareTo method of String class which takes the method parameter type is String and it returns int type. The comparison is based on the Unicode value of each character in the strings.

For example, take two strings as below.

String s1 = "abcdf";
String s2 = "abceg";

Take the each index character value from these two strings and compare them. Assume k is index, if the difference of two numbers is zero then they are said to be same. This is correct. Saying they are same in different way than comparing is using == operator.


If the difference is zero for the same index in two string then will go for the next index. This process repeats untill it finds non zero difference or if difference is zero then minimum length of string1 and string 2. This comparison process is called as Lexicographically Comparison.

Example Simulation:

This works based on Unicode value. For easy understanding just like dictionary or alphabetical order.

ASCII code of a = 97, b = 98, c = 99, d = 100, e = 101

s1[0] - s2[0] ==> a - a => 97 - 97 ==> 0
s1[1] - s2[1] ==> b - b => 98 - 98 ==> 0
s1[2] - s2[2] ==> c - c => 99 - 99 ==> 0
s1[3] - s2[3] ==> d - e => 100 - 101 ==> -1

It stops comparing at this point. If diff is non zero then return the difference. It will not check for remaining indexes even though same or not.

Syntax: public int compareTo(String anotherString)

ComareTo method internal code:

This code is from java String api class internal code for better visibility how it has been implemented.
Try to understand code below.

public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
        return len1 - len2;

String class has char array variable named value. Lexicographically means comparing two strings character by character and when it mismatches then it return difference of two characters.

Read more about How to make a class Immutable in java.

CompareTo Step By Step internal Explanation:

How String compareTo method works internally in java.

String s1 = "abcd";
String s2 = "abce";

Step 1:
CompareTo method works always on two strings.

Step 2:
If any one of String is null then will throw runtime exception saying NullPointerException.
Exception in thread "main" java.lang.NullPointerException
  at java.lang.String.compareTo(Unknown Source)
  at blog.java.w3schools.string.compare.lexicographically.StringCompareLexicographically.main(StringCompareLexicographically.java:10)

Step 3:
String class has a instance variable that is named "value" which is char[] array.

Step 4:
Get the length of two strings.
 int len1 = s1.value.length;
        int len2 = s2.value.length;

Step 5:
Get the minimum length from len1, len2. This is said to be limit for the comparison.
int lim = Math.min(len1, len2);

Step 6:
Get two arrays.
 char v1[] = s1.value;
        char v2[] = s2.value;

Step 7:
loop from index 0 to lim for both strings s1, s2.

Step 8:
Compare each index value. If not equal then return diff.

Step 9:
If both strings lengths are same then comparison should be done till last index or till not same.

Step 10:
If lenghts are not same then compare the min lenght index and if both string are same till min length then return diff len1 - len2.
  String s1 = "pqrs";
  String s2 = "pqrstuv";
  lenght = min (s1 length, s2 length) ==> (4, 3) ==> first four characters are same ==> then compare len1 - len2 ==> 4-7 ==> -3

Compare two String Lexicographically Example:

package blog.java.w3schools.string.compare.lexicographically;

public class StringCompareLexicographically {

 public static void main(String[] args) {

  String s1 = "abcd";
  String s2 = "abce";

  int diff = s1.compareTo(s2);
  System.out.println("Compare Lexicographically diff : " + diff);

Compare Lexicographically diff : -1



Comparing two strings lexicographically plays significant role in string sorting techniques in java. lexicographically means comparing each character in both string index by index. It uses unicode of character (ASCII). If all characters are same then it returns 0. If any mismatch is found then returns difference.

Please leave your understanding, Any comments, Questions in comments box.



About Us

Author: Venkatesh - I love to learn and share the technical stuff.

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,14,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,22,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,80,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,4,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,9,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,66,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,77,java.lang,5,java.util. function,1,jQuery,1,Kotlin,10,Kotlin Conversions,3,Kotlin Programs,6,Lambda,1,lang,29,Leap Year,1,live updates,1,Logging,1,Mac OS,2,Math,1,Matrix,5,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,1,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,21,String,58,String Programs,10,String Revese,1,Swing,1,System,1,Tags,1,Threads,10,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,2,Updates,3,util,5,While Loop,1,
JavaProgramTo.com: Java Compare Two Strings Lexicographically | String compareTo method works internally
Java Compare Two Strings Lexicographically | String compareTo method works internally
How to compare two string in java Lexicographically, Lexicographically meaning in java, How CompareTo method works internally, Example with Step by step explanation.
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content