Using compareTo() when you need to know the difference in length between strings that start with the same sequence of characters.
/**
* @author lautturi.com
* Java example: difference between compareto and equals in java
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
System.out.println("hello".compareTo("olleh"));
System.out.println("hello".equals("olleh"));
System.out.println("hello".compareTo("hallo"));
}
}
output:
-7 false 4
e - a = 4.