compareTo()Java中的方法返回一个大于/等于/小于0的值,我就知道了.但是,价值本身就是我的问题.compareTo()返回时2或4之间有什么区别.看下面的代码
String s1="hello";
String s2="hello";
String s3="meklo";
String s4="hemlo";
System.out.println(s1.compareTo(s2)); // 0
System.out.println(s1.compareTo(s3)); // -5
System.out.println(s1.compareTo(s4)); // -1
Run Code Online (Sandbox Code Playgroud)
为什么最后两个命令是-5和-1?
比较不要给我-10更大的论点.(当字符串与参数相等时,我得到0).
String str2 = "Strings are immutable";
String str3 = "Int is a";
int result = str3.compareTo( str2 );
System.out.println(result);
result = str2.compareTo( str3 );
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
如果更改str2或str3的大小,则返回编号相同.这是为什么?