the CompareTo method in Java, returns the difference between the ASC codes of the previous two strings participating in the comparison, and the following set of codeString a= "A", b= "B";System.out.println (A.COMPARETO.B);then output-1;if A= "a", b= "a" is output 0;if a= "B", the b= "a" is output 1; when a single character is compared, what if the string is longer?? if a= "AB", b= "B", then output-1;if A= "abcdef", b= "B" is output-1;That is
, if the first letter of the two string is different, the method returns the difference of the first letter of the ASC code; What if the first letter is the same?? if a= "AB", b= "a", Output 1;if A= "abcdef", b= "a" output 5;if A= "abcdef", b= "abc" Output 3;if A= "abcdef", b= "Ace" output-1;
that is, the two strings participating in the comparison, if the first character is the same, the next character is compared until there are different, the ASC code difference of the different characters is returned, if the two strings are not the same length, the characters that can participate in the comparison are identical, the length difference of two strings is returned.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Usage of CompareTo in Java