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

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


当前回答

操作符==总是用于对象引用比较,而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)

其他回答

在Java中,当使用==运算符比较两个对象时,它会检查两个对象是否指向内存中的相同位置。换句话说,它检查两个对象名称是否基本上引用了相同的内存位置。

Java String类实际上重写了Object类中的默认equals()实现——它重写了该方法,以便它只检查字符串的值,而不检查它们在内存中的位置。 这意味着如果调用equals()方法来比较两个String对象,那么只要实际的字符序列相等,两个对象就被认为是相等的。

==操作符检查两个字符串是否完全相同的对象。

equals()方法检查两个字符串是否具有相同的值。

==执行引用相等性检查,2个对象(在本例中为字符串)是否引用内存中的同一对象。

equals()方法检查两个对象的内容或状态是否相同。

显然==更快,但如果你只是想知道2个字符串是否包含相同的文本,在许多情况下会(可能)给出错误的结果。

当然推荐使用equals()方法。

不要担心性能。使用String.equals()有以下几点值得鼓励:

Implementation of String.equals() first checks for reference equality (using ==), and if the 2 strings are the same by reference, no further calculation is performed! If the 2 string references are not the same, String.equals() will next check the lengths of the strings. This is also a fast operation because the String class stores the length of the string, no need to count the characters or code points. If the lengths differ, no further check is performed, we know they cannot be equal. Only if we got this far will the contents of the 2 strings be actually compared, and this will be a short-hand comparison: not all the characters will be compared, if we find a mismatching character (at the same position in the 2 strings), no further characters will be checked.

当一切都说了,做了,即使我们可以保证字符串是实习生,使用equals()方法仍然不是人们可能认为的开销,绝对是推荐的方式。如果您想要有效的引用检查,那么在语言规范和实现保证相同enum值将是相同对象(通过引用)的地方使用enum。

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

.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()方法将检查两个字符串是否具有相同的值。

==比较对象的引用值,而java.lang.String类中的equals()方法比较String对象的内容(与另一个对象)。