STRING INSTANCE METHODS

Java String contentEquals() method

contentEquals() compares a String string with another character sequence. It is especially useful for comparing an immutable String with a mutable StringBuilder or StringBuffer.

Comparing text contents

String tekstas = "Java kursas";
StringBuilder kuriamasTekstas = new StringBuilder("Java kursas");

System.out.println(tekstas.contentEquals(kuriamasTekstas));

kuriamasTekstas.append(" internetu");
System.out.println(tekstas.contentEquals(kuriamasTekstas));
Console output

true
false

  1. tekstas stores the immutable text Java kursas.
  2. kuriamasTekstas is a separate StringBuilder object, but initially contains the same characters.
  3. contentEquals() compares characters rather than object types or references, so the first result is true.
  4. append() appends this text to the builder: internetu.
  5. The sequences now differ, so the second comparison returns false.

Method syntax and overloads

String class has two contentEquals() overloads:

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

Parts of the contentEquals() method
PartMeaning
Calling objecttekstas in the call tekstas.contentEquals(...)
ParameterStringBuffer or the more general CharSequence
Return typeboolean
trueThe lengths and all characters at corresponding positions match
falseAt least one character or the sequence length differs
ErrorNullPointerExceptionwhen the argument is null

StringBuilder, StringBuffer and String implement CharSequence, so they can be passed as the sequence to compare.

Why is contentEquals() an instance method?

contentEquals() checks a specific String object's contents, so it is called on that string:

boolean sutampa = tekstas.contentEquals(builder);

On the left, tekstas is the base character sequence, while builder is passed as an argument. We therefore do not write String.contentEquals(...) : the method needs a specific String instance whose characters it can compare.

Comparing StringBuilder and StringBuffer

String kodas = "JAVA-21";
StringBuilder builder = new StringBuilder("JAVA-21");
StringBuffer buffer = new StringBuffer("JAVA-21");

System.out.println(kodas.contentEquals(builder));
System.out.println(kodas.contentEquals(buffer));
Console output

true
true

  1. kodas, builder and buffer are objects of different types.
  2. All three objects contain the same seven characters, JAVA-21.
  3. The first call accepts StringBuilder as a CharSequence.
  4. The second call accepts StringBuffer.
  5. In both cases, the lengths, characters and order match, giving true.

The difference between contentEquals() and equals()

String tekstas = "Labas";
StringBuilder builder = new StringBuilder("Labas");

System.out.println(tekstas.equals(builder));
System.out.println(tekstas.contentEquals(builder));
Console output

false
true

String.equals() returns true only when the compared object is also a String with matching text. contentEquals() supports different CharSequence implementations: it can compare String and StringBuilder by their characters.

Choosing equals() or contentEquals()
SituationSuitable method
Two String objectsUsually equals()
String and StringBuildercontentEquals()
String and StringBuffercontentEquals()
Letter case must be ignoredThis method does not support that; for two String use equalsIgnoreCase()

Character order must match

String tekstas = "Java";
StringBuilder builder = new StringBuilder("avaJ");

System.out.println(tekstas.contentEquals(builder));
Console output

false

Both sequences contain the same letters, but their positions differ. contentEquals() checks the exact sequence from first character to last, so Java and avaJ are not equal.

Uppercase and lowercase letters differ

String tekstas = "JavaQuest";
StringBuffer buffer = new StringBuffer("javaquest");

System.out.println(tekstas.contentEquals(buffer));
Console output

false

contentEquals() has no case-insensitive variant. J and j are different characters, so JavaQuest and javaquest do not match.

Comparing empty sequences

String tuscias = "";
StringBuilder tusciasBuilder = new StringBuilder();
StringBuilder netusciasBuilder = new StringBuilder("Java");

System.out.println(tuscias.contentEquals(tusciasBuilder));
System.out.println(tuscias.contentEquals(netusciasBuilder));
Console output

true
false

Two empty strings have the same length and no differing characters, so they are equal. When the builder contains Java, the lengths differ and the method returns false.

A null argument causes an error

String tekstas = "Java";
CharSequence lyginamas = null;

System.out.println(tekstas.contentEquals(lyginamas));
Result

NullPointerException

  1. lyginamas is declared as a CharSequence, but its value is null.
  2. contentEquals() must read the sequence's length and characters.
  3. null object does not exist, so comparison is impossible and it throws NullPointerException.

Checking safely before comparison

String tekstas = "Java";
CharSequence lyginamas = null;

boolean sutampa = lyginamas != null
    && tekstas.contentEquals(lyginamas);

System.out.println(sutampa);
Console output

false

First, the code checks lyginamas != null. Since it is false, the short-circuit operator && skips the second condition, so contentEquals() is not called and no error occurs.

Common mistakes

  • Using equals() expecting String and StringBuilder with the same text to be equal.
  • Expecting contentEquals() to ignore uppercase and lowercase differences.
  • Passing a null argument, producing a NullPointerException.
  • contentEquals() is confused with contains(): the first checks entire-content equality; the second searches for a substring.

When should you use contentEquals()?

Use contentEquals()when you have a String and want to compare its contents directly with a StringBuilder, StringBuffer or another CharSequence. If both values are String, it is usually clearer to use equals(). To find only part of a string, use contains().