曾记否我们在JavaSE-String API 其他功能接触的compareTo,今天我们深入源代码一探究竟。
请看这样一段代码:
public class CompareToTest {
/**
* 权兴权意-2016.11.17
* JavaSE-String API compareTo源代码分析
*/
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hel";
int diff = s1.compareTo(s2);
System.out.println(diff);
}
}
大家猜猜会输出啥?
由之前的经验,一个个比较,最后会输出一个数字,这个数字是‘l’-‘’的值。
然而真的是这样吗?
运行,输出了一个2。
什么情况?当你不知道是什么情况时,最好的方法就是读源代码。
/**
* Compares two strings lexicographically.
* The comparison is based on the Unicode value of each character in
* the strings. The character sequence represented by this
* <code>String</code> object is compared lexicographically to the
* character sequence represented by the argument string. The result is
* a negative integer if this <code>String</code> object
* lexicographically precedes the argument string. The result is a
* positive integer if this <code>String</code> object lexicographically
* follows the argument string. The result is zero if the strings
* are equal; <code>compareTo</code> returns <code>0</code> exactly when
* the {@link #equals(Object)} method would return <code>true</code>.
* <p>
* 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 <i>k</i> be the smallest such index; then the string
* whose character at position <i>k</i> has the smaller value, as
* determined by using the < operator, lexicographically precedes the
* other string. In this case, <code>compareTo</code> returns the
* difference of the two character values at position <code>k</code> in
* the two string -- that is, the value:
* <blockquote><pre>
* this.charAt(k)-anotherString.charAt(k)
* </pre></blockquote>
* If there is no index position at which they differ, then the shorter
* string lexicographically precedes the longer string. In this case,
* <code>compareTo</code> returns the difference of the lengths of the
* strings -- that is, the value:
* <blockquote><pre>
* this.length()-anotherString.length()
* </pre></blockquote>
*
* @param anotherString the <code>String</code> to be compared.
* @return the value <code>0</code> if the argument string is equal to
* this string; a value less than <code>0</code> if this string
* is lexicographically less than the string argument; and a
* value greater than <code>0</code> if this string is
* lexicographically greater than the string argument.
*/
public int compareTo(String anotherString) {
int len1 = count;
int len2 = anotherString.count;
int n = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
if (i == j) {
int k = i;
int lim = n + i;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
} else {
while (n-- != 0) {
char c1 = v1[i++];
char c2 = v2[j++];
if (c1 != c2) {
return c1 - c2;
}
}
}
return len1 - len2;
}
其实扫了一眼就秒懂了,当遇到字符不等时,返回他们的差值:
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
当他们相等时,返回长度的差值,自然输出2。
return len1 - len2;
细水长流,我们不妨通过断点或者脑动运行一遍,运行结果见注释。
public int compareTo(String anotherString) {
int len1 = count;//5
int len2 = anotherString.count;//3
int n = Math.min(len1, len2);//3
char v1[] = value;//"hello"
char v2[] = anotherString.value;//"hel"
int i = offset;//0
int j = anotherString.offset;//0
if (i == j) {//0==0
int k = i;//k=0
int lim = n + i;//lim=3
while (k < lim) {//0<3,1<3,2<3,3=3
char c1 = v1[k];//h,e,l
char c2 = v2[k];//h,e,l
if (c1 != c2) {
return c1 - c2;
}
k++;//1,2,3
}
} else {
while (n-- != 0) {
char c1 = v1[i++];
char c2 = v2[j++];
if (c1 != c2) {
return c1 - c2;
}
}
}
return len1 - len2;//2
}