简体   繁体   中英

Java compareTo step by step execution

I have basic understanding that Java compareTo method lexicographically compares two strings. I have read basics here String Comparison in Java

I have following example:

public class CompareTo {
   public static void main(String args[]) {
       String str1 = "String";
       String str2 = "compareTo";
       String str3 = "String";
       int var1 = str1.compareTo( str2 );
       System.out.println("str1 & str2 comparison: "+var1);

       int var2 = str1.compareTo( str3 );
       System.out.println("str1 & str3 comparison: "+var2);
   }
}

I get var1 = -16 and var2 = 0 .

If someone explains me this Lexicographic comparison step by step It would be of great help.

Thanks.

Well, if you print :

System.out.println ('S'-'c');

you get -16

String 's compareTo compares the two String s one character at a time. The first pair of characters that are not equal (in your case 'S' of "String" and 'c' of "compareTo") determines the result. Since lexicographically 'S' comes before 'c', the comparison returns a negative value, which means "String" should come before "compareTo".

In your second comparison, all pairs of characters are equal, so compareTo returns 0.

From the Java DOC

public int compareTo(String anotherString)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

this.charAt(k)-anotherString.charAt(k)

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

this.length()-anotherString.length()

In the ASCII table c = 99 (decimal) and S = 83 ( decimal)

for that: S - c = -16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM