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]
本文详细解析了Java中字符串的比较方法和比较运算符的使用,包括字符串对象之间的相等性判断、字符串长度比较以及字符串的顺序比较等关键概念。通过实例演示,帮助读者理解并掌握Java字符串的比较机制。
1811

被折叠的 条评论
为什么被折叠?



