Java String contentEquals() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, contentEquals() checks whether this string has the same characters as a StringBuilder, StringBuffer, or other CharSequence.

You pass the other sequence. It is required. The compare is case-sensitive. Use this when equals() would fail because the other object is not a String.

Syntax

Call contentEquals on the string. Start with this line.

java
boolean ok = text.contentEquals(sb);
CallWhat it does
text.contentEquals(sb)Returns true when text and sb have the same characters.

equals(sb) is false when sb is a StringBuilder, even if the characters match.

Library declarations
java
public boolean contentEquals(StringBuffer sb);
public boolean contentEquals(CharSequence cs);

Parameters

ParameterDescription
csRequired. The StringBuilder, StringBuffer, or CharSequence to compare.

Return Value

Return ValueDescription
trueThe characters match.
falseThe characters do not match.

Java Examples

Example 1 — Match a StringBuilder

Compare a fixed string with text built in a buffer.

java
public class ContentEqualsBasic {
    public static void main(String[] args) {
        String expected = "Code";
        StringBuilder built = new StringBuilder();
        built.append("Co").append("de");

        boolean ok = expected.contentEquals(built);

        System.out.println("contentEquals = " + ok);
    }
}

How It Works

  1. Start with a target. expected holds "Code".
  2. Build the same text. Append "Co" then "de" into a StringBuilder.
  3. Call contentEquals. Characters are compared in order.
  4. Get true. No toString() was required.

Example 2 — equals vs contentEquals

equals() rejects a builder even when the text matches.

java
public class ContentEqualsVsEquals {
    public static void main(String[] args) {
        String s = "Hi";
        StringBuilder sb = new StringBuilder("Hi");

        System.out.println("equals        = " + s.equals(sb));
        System.out.println("contentEquals = " + s.contentEquals(sb));
    }
}

How It Works

  1. Same characters. Both hold H then i.
  2. equals fails. The argument is not a String, so content is never compared.
  3. contentEquals succeeds. It accepts any CharSequence.
  4. Choose by type. Use equals for two strings; use contentEquals for builders.

Example 3 — Case Still Matters

Content match is exact. For ignore-case on two strings, use equalsIgnoreCase().

java
public class ContentEqualsCase {
    public static void main(String[] args) {
        String s = "Java";
        StringBuilder sb = new StringBuilder("java");

        System.out.println(s.contentEquals(sb));
        System.out.println(s.equalsIgnoreCase(sb.toString()));
    }
}

How It Works

  1. Different case. "Java" vs "java".
  2. contentEquals is exact. 'J' ≠ 'j' → false.
  3. Need ignore-case? Convert the builder (or use two strings) and call equalsIgnoreCase.
  4. Pick the right tool. Content match ≠ case-insensitive match.

Frequently Asked Questions

contentEquals() compares this string's characters with a StringBuilder, StringBuffer, or CharSequence.
cs is required. It is the other character sequence.
equals() expects another String. contentEquals() can compare with builders and other CharSequence values.
Yes.
NullPointerException is thrown.

Next: copyValueOf()

Create a String from a character array (or a slice of one).

copyValueOf() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful