How to convert a char array back to a string?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java, converting a char[] back to a String is usually done with the String constructor or String.valueOf(). The operation is simple, but it is worth understanding the tradeoffs around copying, slicing, and why char[] is sometimes used in the first place for sensitive data such as passwords.
Core Sections
The direct Java answer
The standard approach is to construct a new String from the character array.
This is the clearest answer when the source is already a char[] and the goal is a normal Java String.
String.valueOf() does the same job clearly
String.valueOf(char[]) is another idiomatic option.
For most application code, new String(chars) and String.valueOf(chars) are both fine. Choose the one your codebase finds clearer.
Convert only part of the array when needed
Sometimes the array is larger than the logical text or you only want a slice. In that case, use the constructor that accepts offset and length.
This builds the string from a portion of the array rather than the whole thing.
Why char[] exists in the first place
A lot of Java code uses char[] instead of String for sensitive values such as passwords. The reason is that a String is immutable, so once created it cannot be cleared. A character array can be overwritten after use.
The important caveat is that once you convert the char[] into a String, the sensitive value now exists as an immutable string object too. That can defeat the original security reason for using a character array.
Strings and character arrays serve different roles
A String is easier to pass around, compare, print, and use with APIs. A char[] is lower-level and mutable. So the conversion question often points to a larger design decision:
- do you want an ordinary text value for application logic
- or do you want a mutable character buffer for controlled handling
For normal text processing, strings are the natural target. For sensitive temporary data, avoid converting unless you truly need to.
Common Pitfalls
- Converting a password
char[]into aStringtoo early removes much of the security benefit of storing it as a mutable array. - Forgetting that Java strings are immutable can lead to incorrect assumptions about clearing or overwriting the converted text.
- Using the full-array constructor when only a portion should be converted produces extra characters in the result.
- Assuming Java’s
char[]conversion behaves like C strings ignores that Java arrays are length-aware and do not use null-termination as string boundaries. - Overcomplicating the conversion when
new String(chars)orString.valueOf(chars)is already the standard solution makes the code harder to read.
Summary
- In Java, convert
char[]toStringwithnew String(chars)orString.valueOf(chars). - Use the offset-and-length constructor when only part of the array should be converted.
- Remember that converting sensitive character data into a
Stringmakes it immutable and harder to clear. - '
char[]andStringare for different purposes: mutability versus convenient text handling.' - For ordinary code, use the simplest conversion method that matches the needed slice of the array.

