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

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


当前回答

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

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

其他回答

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

.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 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()方法,因为Object包含一个返回布尔值的方法。equals()。如果需要进一步的定义,子类的工作就是重写这个方法。如果没有它(即使用==),则只检查两个对象之间的内存地址是否相等。String重写了这个.equals()方法,它没有使用内存地址,而是返回字符级别的字符串的相等性比较。

A key note is that strings are stored in one lump pool so once a string is created it is forever stored in a program at the same address. Strings do not change, they are immutable. This is why it is a bad idea to use regular string concatenation if you have a serious of amount of string processing to do. Instead you would use the StringBuilder classes provided. Remember the pointers to this string can change and if you were interested to see if two pointers were the same == would be a fine way to go. Strings themselves do not.

是的,==不利于比较字符串(实际上是任何对象,除非您知道它们是规范的)。==只是比较对象引用。.equals()测试是否相等。对于字符串,它们通常是相同的,但正如你所发现的,这并不总是保证的。

如果你像我一样,当我第一次开始使用Java时,我想使用“==”操作符来测试两个String实例是否相等,但无论如何,这不是在Java中正确的方法。

在本教程中,我将演示几种不同的正确比较Java字符串的方法,从我最常用的方法开始。在本Java字符串比较教程的最后,我还将讨论为什么“==”操作符在比较Java字符串时不起作用。

选项1:使用equals方法进行Java字符串比较 大多数时候(可能是95%的时间),我用Java String类的equals方法比较字符串,像这样:

if (string1.equals(string2))

String equals方法查看两个Java字符串,如果它们包含完全相同的字符串,则认为它们相等。

看一下一个使用equals方法的快速字符串比较示例,如果运行下面的测试,两个字符串不会被认为是相等的,因为字符不完全相同(字符的大小写不同):

String string1 = "foo";
String string2 = "FOO";

if (string1.equals(string2))
{
    // this line will not print because the
    // java string equals method returns false:
    System.out.println("The two strings are the same.")
}

但是,当两个字符串包含完全相同的字符串时,equals方法将返回true,如下例所示:

String string1 = "foo";
String string2 = "foo";

// test for equality with the java string equals method
if (string1.equals(string2))
{
    // this line WILL print
    System.out.println("The two strings are the same.")
}

选项2:使用equalsIgnoreCase方法进行字符串比较

在一些字符串比较测试中,您可能希望忽略字符串是大写还是小写。当你想以这种不区分大小写的方式测试字符串是否相等时,使用String类的equalsIgnoreCase方法,如下所示:

String string1 = "foo";
String string2 = "FOO";

 // java string compare while ignoring case
 if (string1.equalsIgnoreCase(string2))
 {
     // this line WILL print
     System.out.println("Ignoring case, the two strings are the same.")
 }

选项3:使用compareTo方法进行Java字符串比较

还有第三种比较Java字符串的不太常见的方法,那就是使用String类的compareTo方法。如果两个字符串完全相同,compareTo方法将返回0(零)的值。下面是这个String比较方法的一个快速示例:

String string1 = "foo bar";
String string2 = "foo bar";

// java string compare example
if (string1.compareTo(string2) == 0)
{
    // this line WILL print
    System.out.println("The two strings are the same.")
}

当我在写Java中的这个相等概念时,有一点很重要:Java语言在基本Java Object类中包含了一个equals方法。每当您创建自己的对象,并且希望提供一种方法来查看对象的两个实例是否“相等”时,您应该在类中重写(并实现)这个equals方法(与Java语言在String equals方法中提供这种相等/比较行为的方式相同)。

你可能想看一下==,.equals(), compareTo()和compare()