String equals() Vs contentEquals() in Java – Java中的String equals() Vs contentEquals()

最后修改: 2022年 1月 3日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

The equals() and the contentEquals() methods of the String class in Java are used to perform String comparison. However, there exist specific differences between the functionalities of these two methods.

Java中Stringequals() contentEquals()方法用于执行String比较。然而,这两个方法的功能存在着具体的差异。

In this tutorial, we’ll take a quick look at the differences between these two methods using practical examples.

在本教程中,我们将通过实际例子快速了解这两种方法的区别。

2. The equals() Method

2.equals()方法

The equals() method is a public method of the Java String class. It overrides the original equals() method from the Object class. The signature of this method is:

equals()方法是Java String类的一个public方法。它覆盖了来自Object类的原始equals()方法。这个方法的签名是。

public boolean equals(Object anObject)

The method compares two different Strings by checking individual characters in both. However, the method not only checks for the content, but also checks if the object is an instance of String. Therefore, the method only returns true if all these conditions are satisfied:

该方法通过检查两个不同的Strings中的单个字符来比较。然而,该方法不仅要检查内容,还要检查对象是否是String的实例。因此,该方法只在所有这些条件都得到满足时返回true

  • the argument object is not null
  • it’s a String object
  • the sequence of characters are identical

3. The contentEquals() Method

3.contentEquals()方法

Similar to the equals() method, the contentEquals() method is also used to compare the String’s content. However, unlike the equals() method, contentEquals() takes any implementation of the CharSequence interface as an argument. That means String, StringBuffer, StringBuilder, CharBuffer, or Segment can be compared.

equals()方法类似,contentEquals()方法也被用来比较String的内容。然而,与equals()方法不同,contentEquals()可以接受任何CharSequence接口的实现作为一个参数。这意味着StringStringBufferStringBuilderCharBufferSegment可以被比较。

The signature of this method is:

这个方法的签名是。

public boolean contentEquals(StringBuffer sb)
public boolean contentEquals(CharSequence cs)

Therefore, the contentEquals() method is only concerned with the content of the string. If the argument is a String object, the equals() method is called for comparison. On the other hand, if a generic character sequence is provided, the method compares individual characters in similar positions.

因此,contentEquals()方法只关注字符串的内容。如果参数是一个String对象,就会调用equals()方法进行比较。另一方面,如果提供了一个通用的字符序列,该方法会比较相似位置的各个字符。

The method returns true if the character sequence in the given argument matches the original String. Unlike the equals() method, if a null argument is passed to the contentEquals() method, it throws a NullPointerException.

如果给定参数中的字符序列与原始String相匹配,则该方法返回true。与equals()方法不同,如果一个null参数被传递给contentEquals()方法,它将抛出一个NullPointerException

4. Examples

4.实例

Let’s see these two methods in action by writing simple test cases. For the sake of simplicity, let’s use the word “Baeldung” for our code.

让我们通过编写简单的测试案例来看看这两种方法的作用。为了简单起见,让我们用 “Baeldung “一词来表示我们的代码。

First, we’ll take two identical String objects and check them. In this case, both methods will return a true value:

首先,我们将取两个相同的String对象并对其进行检查。在这种情况下,两个方法都将返回一个值。

String actualString = "baeldung";
String identicalString = "baeldung";

assertTrue(actualString.equals(identicalString));
assertTrue(actualString.contentEquals(identicalString));

Next, we take two different implementations of CharSequence with identical content. For the first implementation, we’ll instantiate CharSequence with a String. In this case, both methods should return true as the content and the types are identical:

接下来,我们采取两个内容相同的CharSequence的不同实现。对于第一个实现,我们将用一个String来实例化CharSequence。在这种情况下,两个方法都应该返回true,因为内容和类型是相同的。

CharSequence identicalStringInstance = "baeldung";

assertTrue(actualString.equals(identicalStringInstance));
assertTrue(actualString.contentEquals(identicalStringInstance));

For the next example, we’ll take a StringBuffer implementation. Since the contentEquals() method only checks for the content, it should return true. However, the equals() method should false:

在下一个例子中,我们将采取一个StringBuffer实现。由于contentEquals()方法只检查内容,它应该返回true然而,equals()方法应该false

CharSequence identicalStringBufferInstance = new StringBuffer("baeldung");

assertFalse(actualString.equals(identicalStringBufferInstance));
assertTrue(actualString.contentEquals(identicalStringBufferInstance));

5. Conclusion

5.总结

In this article, we took a quick look at the two methods of the String class. While the equals() method only compares instances of String, the contentEquals() method can compare any implementation of CharSequence.

在这篇文章中,我们快速看了一下String类的两个方法。equals()方法只比较String的实例,而contentEquals()方法可以比较CharSequence的任何实现。

To conclude, we should use contentEquals() when we are only concerned about the content of the object. On the other hand, sometimes it might be important to check for the type of the object. In that case, we should use the equals() method which gives us stricter checking conditions.

总而言之,当我们只关心对象的内容时,我们应该使用contentEquals()。另一方面,有时检查对象的类型可能很重要。在这种情况下,我们应该使用equals()方法,它为我们提供了更严格的检查条件。

As always, the code snippets are available over on GitHub.

像往常一样,代码片段可在GitHub上获得