A= "abcd "
B= "abcd "
A==B 是返回 true. 因为A,B都一样.
A=new String( "abcd ")
B=new String( "abcd ")
A==B 则返回 flase
而 A.equals(B) 输出为 true
String s1 = "abc";
String s2 = "cd";
int r = s2.compareTo(s1);
System.out.println("r = " + r);
输出为 r = 2
int r = s1.compareTo(s2);
输出为 r = -2
String s1 = "abc";
String s2 = "bc";
int r = s2.compareTo(s1);
输出为 r = 1
int r = s1.compareTo(s2);
输出为 r = -1
String s1 = "abc";
String s2 = "abc";
int r = s2.compareTo(s1);
输出为 r = 0
String s1 = "abc";
String s2 = "ab";
int r = s2.compareTo(s1);
输出为 r = -1
int r = s1.compareTo(s2);
输出为 r = 1
String s1 = "abc";
String s2 = "gab";
int r = s1.compareTo(s2);
输出为 r = -6
String s1 = "abc";
String s2 = "fabc";
int r = s1.compareTo(s2);
输出为 r = -5
相关链接:[url]http://leepoint.net/notes-java/data/expressions/22compareobjects.html[/url]
B= "abcd "
A==B 是返回 true. 因为A,B都一样.
A=new String( "abcd ")
B=new String( "abcd ")
A==B 则返回 flase
而 A.equals(B) 输出为 true
String s1 = "abc";
String s2 = "cd";
int r = s2.compareTo(s1);
System.out.println("r = " + r);
输出为 r = 2
int r = s1.compareTo(s2);
输出为 r = -2
String s1 = "abc";
String s2 = "bc";
int r = s2.compareTo(s1);
输出为 r = 1
int r = s1.compareTo(s2);
输出为 r = -1
String s1 = "abc";
String s2 = "abc";
int r = s2.compareTo(s1);
输出为 r = 0
String s1 = "abc";
String s2 = "ab";
int r = s2.compareTo(s1);
输出为 r = -1
int r = s1.compareTo(s2);
输出为 r = 1
String s1 = "abc";
String s2 = "gab";
int r = s1.compareTo(s2);
输出为 r = -6
String s1 = "abc";
String s2 = "fabc";
int r = s1.compareTo(s2);
输出为 r = -5
相关链接:[url]http://leepoint.net/notes-java/data/expressions/22compareobjects.html[/url]