到目前为止,我一直在程序中使用==运算符来比较所有的字符串。 然而,我遇到了一个错误,将其中一个改为.equals(),它修复了这个错误。
==不好吗?什么时候应该使用,什么时候不应该使用?有什么不同?
到目前为止,我一直在程序中使用==运算符来比较所有的字符串。 然而,我遇到了一个错误,将其中一个改为.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有一个String池,Java在该池下管理String对象的内存分配。参见Java中的字符串池
当您使用==操作符检查(比较)两个对象时,它将地址相等性比较到字符串池中。如果两个String对象具有相同的地址引用,则返回true,否则返回false。但是如果你想比较两个String对象的内容,那么你必须重写equals方法。
equals实际上是Object类的方法,但它被重写到String类中,并给出了一个比较Object内容的新定义。
Example:
stringObjectOne.equals(stringObjectTwo);
但注意,它尊重String的情况。如果你想要不区分大小写的比较,那么你必须使用String类的equalsIgnoreCase方法。
让我们来看看:
String one = "HELLO";
String two = "HELLO";
String three = new String("HELLO");
String four = "hello";
one == two; // TRUE
one == three; // FALSE
one == four; // FALSE
one.equals(two); // TRUE
one.equals(three); // TRUE
one.equals(four); // FALSE
one.equalsIgnoreCase(four); // TRUE
Java中的字符串是不可变的。这意味着每当您尝试更改/修改字符串时,都会得到一个新实例。你不能改变原来的字符串。这样做是为了缓存这些字符串实例。典型的程序包含大量字符串引用,缓存这些实例可以减少内存占用并提高程序的性能。
当使用==运算符进行字符串比较时,您不是在比较字符串的内容,而是在比较内存地址。如果两者相等,则返回true,否则返回false。而字符串中的equals则比较字符串内容。
问题是,如果所有字符串都缓存在系统中,为什么==返回false,而==返回true?这是可能的。如果你创建一个新的字符串,比如string str = new string ("Testing"),你最终会在缓存中创建一个新的字符串,即使缓存中已经包含了一个具有相同内容的字符串。总之,"MyString" == new String("MyString")将总是返回false。
Java还讨论了intern()函数,该函数可用于字符串,使其成为缓存的一部分,因此"MyString" == new string ("MyString").intern()将返回true。
注意:==操作符比equals快得多,因为您比较的是两个内存地址,但您需要确保代码没有在代码中创建新的String实例。否则会遇到bug。
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”的新引用。参考文献不同,但内容是一样的。