定义
-
equals()
- method
- 比较两个对象的内容是否相同
-
compareTo()
- method
- Syntax :
int compareTo(Object obj) - Parameters :
obj : the Object to be compared. - Return Value :
The value 0 if the argument is a string lexicographically equal to this string;
a value less than 0 if the argument is a string lexicographically greater than this string;
and a value greater than 0 if the argument is a string lexicographically less than this string.
-
==
- operator
- 比较两个对象的指向的地址是否相同
-
equalsIgnoreCase()
- Syntax : 同equals()
- 区别:比较时忽略大小写
-
compare()
- Syntax :
java.text.Collator; - 类似compareTo()
- Syntax :
例
public static void main(String args[])
{
String str1 = "String method tutorial";
String str2 = "compareTo method example";
String str3 = "string method tutorial";
int var1 = str1.compareTo( str2 );
System.out.println("str1 & str2 comparison: "+var1);
int var2 = str1.compareTo( str3 );
System.out.println("str1 & str3 comparison: "+var2);
int var3 = str2.compareTo("compareTo method example");
System.out.println("str2 & string argument comparison: "+var3);
String s1 = new String("HELLO");
String s2 = new String("HELLO");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(s1.compareTo(s2));
}
[@参考文献]
Java | ==, equals(), compareTo(), equalsIgnoreCase() and compare()
compareTo()
作者 @FL
2019 年 04月 24日