How to convert CharSequence to String?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
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):
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:
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:
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
CharSequenceif your API only needs read access
Example of wasteful code:
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:
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
CharSequencetoStringistoString(). - '
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
CharSequenceunless you specifically needStringbehavior or immutability.
Related reading
- How to convert comma-separated String to List?
- How to convert currentTimeMillis to a date in Java?
- How to convert float to int with Java
- How to convert hashmap to JSON object in Java
- How to convert int[] into List<Integer> in Java?
- How to convert Java String into byte[]?
- How to convert java.sql.timestamp to LocalDate java8 java.time?
- How to convert java.util.Date to java.sql.Date?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.