*对于数字对象的比较
Integer x=5; Integer y=88; Integer z=5; Integer w=3; System.out.println(x.compareTo(3)); System.out.println(x.compareTo(5)); System.out.println(x.compareTo(88)); System.out.println(x.compareTo(y)); System.out.println(x.compareTo(z)); System.out.println(x.compareTo(w));
输出值为:
1 0 -1 -1 0 1
str1.compareTo(str2)
str1 值大于 str2值 返回1;
str1 值等于 str2值 返回0;
str1 值小于 str2值 返回-1;
*关于字符串之间的比较
*-*字符串长度相同
String s1="abcde"; String s2="abdde"; String s3="abccc"; String s4="abedd"; System.out.println(s1.compareTo(s2)); System.out.println(s3.compareTo(s4));
//只是返回第一个不相同的字母的ASCII码的差值,后续的字符串差值不再进行比较
//其中ASCII码值:c(99),d(100),e(101)
返回值为;
-1 -2
*-*字符串长度不同
String s5="abcef"; String s6="abdefgh; System.out.println(s5.compareTo(s6));
//(此时不同的字符位置小于最短字符串长度)同样只是返回第一个不相同的字母的ASCII码的差值
返回值为
-1
String s7="abcd"; String s8="abcdefgh"; System.out.println(s7.compareTo(s8));
//此时 字符串s7的字符与字符串s8 前四位字符相同
//此时返回两个不同长度字符串的长度差值,
//此时的返回值与 ASCII码无关
返回值为
-4