Java compareto() method

The compareTo() method is used to compare the two approaches:
String is compared to an object.
compares two strings in dictionary order.
syntax
int compareTo(Object o)
or
Int compareTo(String anotherString)
parameter
o – object to compare.
AnotherString – The string to compare.
Return value
the return value is the integer, it is first to compare the size of the corresponding character (ASCII), if the first character and the parameters of the first character, end of comparison, return the difference between them, if the first character is equal to the parameters of the first character, with the second character and parameter of the second character, and so on, until the comparison of character or character one end are being compared.
If the parameter string is equal to this string, the value 0 is returned;
returns a value less than 0 if the string is smaller than the string argument;
returns a value greater than 0 if the string is greater than the string argument.
instance
public class Test {

public static void main(String args[]) {
    String str1 = "Strings";
    String str2 = "Strings";
    String str3 = "Strings123";

    int result = str1.compareTo( str2 );
    System.out.println(result);
  
    result = str2.compareTo( str3 );
    System.out.println(result);
 
    result = str3.compareTo( str1 );
    System.out.println(result);
}

}
above program execution result is:
0
-3
3


Read More: