CharSequence
String
Java
Programming
Type Conversion

How to convert CharSequence to String?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting a CharSequence to a String in Java is usually straightforward: call toString(). The subtle part is understanding what CharSequence actually represents and avoiding extra work or incorrect API assumptions.

What CharSequence Is

CharSequence is an interface implemented by several text-like types, including:

  • 'String'
  • 'StringBuilder'
  • 'StringBuffer'
  • 'CharBuffer'

Because the static type is only CharSequence, your code knows it can read characters, but it does not know whether the underlying object is already an immutable String or a mutable buffer.

The Normal Conversion

The standard conversion is:

java
CharSequence seq = new StringBuilder("hello");
String value = seq.toString();
System.out.println(value);

That is the correct and idiomatic answer in nearly every case.

If the underlying object already is a String, toString() simply returns that string. If it is a mutable type such as StringBuilder, toString() creates a string snapshot of the current content.

String.valueOf Is Also Fine

Another common option is String.valueOf(seq):

java
CharSequence seq = new StringBuffer("hello");
String value = String.valueOf(seq);
System.out.println(value);

This is also valid, but it has slightly different null behavior. String.valueOf(null) returns the literal string "null", while calling seq.toString() on a null reference throws NullPointerException.

That means the choice depends on whether null should be treated as a bug or as a value that needs graceful handling.

Why a Constructor Is Not the Answer

One frequent misconception is that Java has a String constructor that accepts any CharSequence. It does not. There are constructors for StringBuilder, StringBuffer, character arrays, and byte arrays, but not a generic String(CharSequence) constructor.

So this does not compile:

java
CharSequence seq = new StringBuilder("hello");
// String value = new String(seq); // does not compile

That is one reason toString() is the correct general-purpose conversion.

Snapshot Semantics

When the source is mutable, conversion creates a stable string representing the current characters at that moment:

java
1StringBuilder builder = new StringBuilder("ab");
2String first = builder.toString();
3
4builder.append("c");
5String second = builder.toString();
6
7System.out.println(first);   // ab
8System.out.println(second);  // abc

This matters in concurrent or staged-processing code. A String is immutable, but many CharSequence implementations are not.

Performance Considerations

Most of the time, conversion cost is negligible. Still, there are two practical rules:

  • do not convert repeatedly inside tight loops unless you need a string each time
  • keep the value as CharSequence if your API only needs read access

Example of wasteful code:

java
for (int i = 0; i < 1000; i++) {
    process(seq.toString());
}

If seq never changes, convert once before the loop.

API Design Guidance

If your method only reads characters, accepting CharSequence is often better than accepting String because it allows callers to pass more types without copying.

Then convert to String only if you need string-specific behavior, long-term storage, or immutability:

java
static String normalize(CharSequence input) {
    return input.toString().trim().toLowerCase();
}

This is a good balance between flexible input and predictable internal processing.

Common Pitfalls

The biggest mistake is using an API that does not exist, such as new String(CharSequence).

Another pitfall is forgetting null handling. toString() is fine when null should fail fast, but String.valueOf behaves differently.

Developers also sometimes assume conversion is always free. It is cheap, but mutable sources such as StringBuilder still need a new immutable string object.

Finally, if an API can work directly with CharSequence, do not force conversion earlier than necessary.

Summary

  • The normal way to convert CharSequence to String is toString().
  • 'String.valueOf(seq) is also valid and differs mainly in null handling.'
  • Java does not provide a generic String(CharSequence) constructor.
  • Conversion from mutable sources creates a string snapshot.
  • Keep values as CharSequence unless you specifically need String behavior or immutability.

Course illustration
Course illustration

All Rights Reserved.