到目前为止,我一直在程序中使用==运算符来比较所有的字符串。 然而,我遇到了一个错误,将其中一个改为.equals(),它修复了这个错误。

==不好吗?什么时候应该使用,什么时候不应该使用?有什么不同?


当前回答

==测试对象引用,.equals()测试字符串值。

有时,==看起来像是比较值,因为Java在幕后做了一些工作,以确保相同的内联字符串实际上是相同的对象。

例如:

String fooString1 = new String("foo");
String fooString2 = new String("foo");

// Evaluates to false
fooString1 == fooString2;

// Evaluates to true
fooString1.equals(fooString2);

// Evaluates to true, because Java uses the same object
"bar" == "bar";

但是要小心空值!

==可以处理空字符串,但从空字符串调用.equals()会导致异常:

String nullString1 = null;
String nullString2 = null;

// Evaluates to true
System.out.print(nullString1 == nullString2);

// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));

因此,如果您知道fooString1可能为空,请通过写入来告诉读取器

System.out.print(fooString1 != null && fooString1.equals("bar"));

下面是更短的,但不太明显,它检查null:

System.out.print("bar".equals(fooString1));  // "bar" is never null
System.out.print(Objects.equals(fooString1, "bar"));  // Java 7 required

其他回答

==测试引用是否相等(它们是否是同一个对象)。

.equals()测试值是否相等(它们是否包含相同的数据)。

Objects.equals()在调用.equals()之前检查是否为空,因此不必这样做(在JDK7中可用,在Guava中也可用)。

因此,如果你想测试两个字符串是否具有相同的值,你可能会使用Objects.equals()。

// These two have the same value
new String("test").equals("test") // --> true 

// ... but they are not the same object
new String("test") == "test" // --> false 

// ... neither are these
new String("test") == new String("test") // --> false 

// ... but these are because literals are interned by 
// the compiler and thus refer to the same object
"test" == "test" // --> true 

// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true

// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true

你几乎总是想使用Objects.equals()。在极少数情况下,您知道您正在处理的是实习字符串,您可以使用==。

来自JLS 3.10.5。字符串:

而且,string字面值总是引用string类的同一个实例。这是因为字符串字面量——或者更一般地说,是常量表达式的值的字符串(§15.28)——被“internned”,以便使用string .intern方法共享唯一的实例。

在JLS 3.10.5-1中也可以找到类似的例子。

其他需要考虑的方法

String.equalsIgnoreCase()忽略大小写的值相等性。但是要注意,在各种与区域设置相关的情况下,此方法可能会产生意想不到的结果,请参阅这个问题。

String. contentequals()将String的内容与任何CharSequence的内容进行比较(自Java 1.5起可用)。在进行相等比较之前,不必将StringBuffer等转换为String,但将null检查留给您。

==比较对象引用。

.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()检查实际的字符串内容(值)。

注意.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.

您还可以使用compareTo()方法来比较两个string。如果compareTo结果为0,则两个字符串相等,否则被比较的字符串不相等。

==比较引用,不比较实际字符串。如果你确实使用new string (somestring).intern()创建了每个字符串,那么你可以使用==操作符来比较两个字符串,否则只能使用equals()或compareTo方法。

如果equals()方法出现在java.lang.Object类中,并且希望它检查对象状态的等价性!也就是说,对象的内容。而==运算符则用于检查实际的对象实例是否相同。

例子

考虑两个不同的参考变量str1和str2:

str1 = new String("abc");
str2 = new String("abc");

如果你使用equals()

System.out.println((str1.equals(str2))?"TRUE":"FALSE");

如果使用==,您将得到TRUE的输出。

System.out.println((str1==str2) ? "TRUE" : "FALSE");

现在您将得到FALSE作为输出,因为str1和str2都指向两个不同的对象,尽管它们共享相同的字符串内容。因为有了new String(),每次都会创建一个新对象。