Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

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.

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

2. The equals() Method

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:

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:

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

3. The contentEquals() Method

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.

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.

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.

4. Examples

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.

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

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 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:

CharSequence identicalStringBufferInstance = new StringBuffer("baeldung");

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

5. Conclusion

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.

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.

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

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.