到目前为止,我一直在程序中使用==运算符来比较所有的字符串。 然而,我遇到了一个错误,将其中一个改为.equals(),它修复了这个错误。
==不好吗?什么时候应该使用,什么时候不应该使用?有什么不同?
到目前为止,我一直在程序中使用==运算符来比较所有的字符串。 然而,我遇到了一个错误,将其中一个改为.equals(),它修复了这个错误。
==不好吗?什么时候应该使用,什么时候不应该使用?有什么不同?
当前回答
==比较对象引用。
.equals()比较字符串值。
有时==会产生比较String值的错觉,如下所示:
String a="Test";
String b="Test";
if(a==b) ===> true
这是因为当您创建任何String字面值时,JVM首先在String池中搜索该字面值,如果找到匹配,则将相同的引用给予新的String。因此,我们得到:
(a==b) ==>为真
String Pool
b -----------------> "test" <-----------------a
但是,==在以下情况下失效:
String a="test";
String b=new String("test");
if (a==b) ===> false
在这种情况下,对于new String("test"),语句new String将在堆上创建,并且该引用将被赋予给b,因此b将被赋予堆上的引用,而不是在String池中。
现在a指向String池中的String,而b指向堆中的String。因此我们得到:
如果(a==b) ==>假。
String Pool
"test" <-------------------- a
Heap
"test" <-------------------- b
而.equals()总是比较String的值,所以在这两种情况下都是true:
String a="Test";
String b="Test";
if(a.equals(b)) ===> true
String a="test";
String b=new String("test");
if(a.equals(b)) ===> true
所以使用.equals()总是更好。
其他回答
是的,==不利于比较字符串(实际上是任何对象,除非您知道它们是规范的)。==只是比较对象引用。.equals()测试是否相等。对于字符串,它们通常是相同的,但正如你所发现的,这并不总是保证的。
功能:
public float simpleSimilarity(String u, String v) {
String[] a = u.split(" ");
String[] b = v.split(" ");
long correct = 0;
int minLen = Math.min(a.length, b.length);
for (int i = 0; i < minLen; i++) {
String aa = a[i];
String bb = b[i];
int minWordLength = Math.min(aa.length(), bb.length());
for (int j = 0; j < minWordLength; j++) {
if (aa.charAt(j) == bb.charAt(j)) {
correct++;
}
}
}
return (float) (((double) correct) / Math.max(u.length(), v.length()));
}
测试:
String a = "This is the first string.";
String b = "this is not 1st string!";
// for exact string comparison, use .equals
boolean exact = a.equals(b);
// For similarity check, there are libraries for this
// Here I'll try a simple example I wrote
float similarity = simple_similarity(a,b);
==操作符检查两个引用是否指向同一个对象。.equals()检查实际的字符串内容(值)。
注意.equals()方法属于类Object(所有类的超类)。您需要根据您的类需求重写它,但是对于String,它已经实现了,并且它检查两个字符串是否具有相同的值。
Case 1 String s1 = "Stack Overflow"; String s2 = "Stack Overflow"; s1 == s2; //true s1.equals(s2); //true Reason: String literals created without null are stored in the String pool in the permgen area of heap. So both s1 and s2 point to same object in the pool. Case 2 String s1 = new String("Stack Overflow"); String s2 = new String("Stack Overflow"); s1 == s2; //false s1.equals(s2); //true Reason: If you create a String object using the new keyword a separate space is allocated to it on the heap.
String a = new String("foo");
String b = new String("foo");
System.out.println(a == b); // prints false
System.out.println(a.equals(b)); // prints true
确保你明白其中的原因。这是因为==比较只比较引用;equals()方法对内容逐字符进行比较。
当你为a和b调用new时,每一个都得到一个指向字符串表中的“foo”的新引用。参考文献不同,但内容是一样的。
操作符==总是用于对象引用比较,而String类.equals()方法用于内容比较被重写:
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2); // It prints false (reference comparison)
System.out.println(s1.equals(s2)); // It prints true (content comparison)