在Java 中,关于String的 compareTo()、equals()和== 的区别

实例为准:

package com.wsy.freemarker;



public class Test {
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("abc");


// == 测试
System.out.println((s1 == s2) ? "true" : "false");


// equals() 方法测试
System.out.println((s1.equals(s2)) ? "true" : "false");


// compareTo() 方法测试
if (s1.compareTo(s2) == 0) {
System.out.println("s1 is equal s2");
}


// 再次验证 ==
s1 = s2;
System.out.println((s1 == s2) ? "true" : "false");
}

}

运行结果:

false
true
s1 is equal s2
true


雪山独孤言:

== 用于比较对象;equals() 与 compareTo()方法用于比较数值, 但equeals() 方法返回的是 boolean 类型,而compareTo() 方法返回的是 int 类型。当需要比较的两个 字符串 的长度不等的时候,equeals() 方法是直接返回false,而compareTo()任然还要去挨个挨个的比较,然后返回差值。


源码参考:

equals() should be the method of choice in the case of the OP.

Looking at the implementation of equals() and compareTo() in java.lang.String on grepcode, we can easily see that equals is better if we are just concerned with the equality of two Strings:

equals():

1012  public boolean equals(Object anObject) {
1013      if (this == anObject) {
1014          return true;
1015      }
1016      if (anObject instanceof String) {
1017          String anotherString = (String)anObject;
1018          int n = count;
1019          if (n == anotherString.count) {
1020              char v1[] = value;
1021              char v2[] = anotherString.value;
1022              int i = offset;
1023              int j = anotherString.offset;
1024              while (n-- != 0) {
1025                  if (v1[i++] != v2[j++])
1026                      return false;
1027              }
1028              return true;
1029          }
1030      }
1031      return false;
1032  }

and compareTo():

1174  public int compareTo(String anotherString) {
1175      int len1 = count;
1176      int len2 = anotherString.count;
1177      int n = Math.min(len1, len2);
1178      char v1[] = value;
1179      char v2[] = anotherString.value;
1180      int i = offset;
1181      int j = anotherString.offset;
1183      if (i == j) {
1184          int k = i;
1185          int lim = n + i;
1186          while (k < lim) {
1187              char c1 = v1[k];
1188              char c2 = v2[k];
1189              if (c1 != c2) {
1190                  return c1 - c2;
1191              }
1192              k++;
1193          }
1194      } else {
1195          while (n-- != 0) {
1196              char c1 = v1[i++];
1197              char c2 = v2[j++];
1198              if (c1 != c2) {
1199                  return c1 - c2;
1200              }
1201          }
1202      }
1203      return len1 - len2;
1204  }

When one of the strings is a prefix of another, the performance of compareTo() is worse as it still needs to determine the lexicographical ordering while equals() won't worry any more and return false immediately.

In my opinion, we should use these two as they were intended:

  • equals() to check for equality, and
  • compareTo() to find the lexical ordering.
尊重他人劳动成果,转载请注明出处。谢谢!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值