我正在检查用于字符串压缩的字符串比较方法并探索反编译器中的字符串类,然后才知道基本上有四种方法
equals()
equalsIgnoreCase()
compareTo()
compareToIgnore()
现在我想知道我们使用的两种方法之间的区别 equals() 和 compareTo() ,基本上请告知为什么 string 类保留了这两种方法..
String tv = "Bravia";
String television = "Bravia";
// String compare example using equals
if (tv.equals(television)) {
System.out.println("Both tv and television contains same letters and equal by equals method of String");
}
// String compare example in java using compareTo
if (tv.compareTo(television) == 0) {
System.out.println("Both tv and television are equal using compareTo method of String");
}
输出 :-
Both tv and television contains same letters and equal by equals method of String
Both tv and television are equal using compareTo method of String