我不知道输出是什么意思?有人会帮我这个吗?请解释一下它是如何工作的。
public class LastIndexOfExample{
public static void main(String args[]){
String s1="hello";
String s2="hello";
String s3="meklo";
String s4="hemlo";
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
System.out.println(s1.compareTo(s4));
}
}
然后输出是: 0 -5 -1
答案 0 :(得分:1)
来自compareTo
javadoc:
如果存在两个字符串不同的索引,则结果为 两个字符在最低指数
之间的差异
完整的Javadoc:
/**
* Compares this string to the given string.
*
* <p>The strings are compared one {@code char} at a time.
* In the discussion of the return value below, note that {@code char} does not
* mean code point, though this should only be visible for surrogate pairs.
*
* <p>If there is an index at which the two strings differ, the result is
* the difference between the two {@code char}s at the lowest such index.
* If not, but the lengths of the strings differ, the result is the difference
* between the two strings' lengths.
* If the strings are the same length and every {@code char} is the same, the result is 0.
*
* @throws NullPointerException
* if {@code string} is {@code null}.
*/
public native int compareTo(String string);
从在线文档(上面的链接):
如果此String对象,则结果为负整数 按字典顺序排列在参数字符串
之前