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.
int compareTo(Object o)
Int compareTo(String anotherString)
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.
public class Test {

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

    int result = str1.compareTo( str2 );
    result = str2.compareTo( str3 );
    result = str3.compareTo( str1 );

above program execution result is:

Read More: