Programming
String Conversion
Char Array
Coding Tutorials
Java

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.

java
1public class Main {
2    public static void main(String[] args) {
3        char[] chars = {'H', 'e', 'l', 'l', 'o'};
4        String text = new String(chars);
5        System.out.println(text);
6    }
7}

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.

java
1public class Main {
2    public static void main(String[] args) {
3        char[] chars = {'J', 'a', 'v', 'a'};
4        String text = String.valueOf(chars);
5        System.out.println(text);
6    }
7}

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.

java
1public class Main {
2    public static void main(String[] args) {
3        char[] chars = {'a', 'b', 'c', 'd', 'e'};
4        String text = new String(chars, 1, 3);
5        System.out.println(text);
6    }
7}

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.

java
1import java.util.Arrays;
2
3public class Main {
4    public static void main(String[] args) {
5        char[] password = {'s', 'e', 'c', 'r', 'e', 't'};
6        String text = new String(password);
7        System.out.println(text.length());
8
9        Arrays.fill(password, '\0');
10    }
11}

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 a String too 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) or String.valueOf(chars) is already the standard solution makes the code harder to read.

Summary

  • In Java, convert char[] to String with new String(chars) or String.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 String makes it immutable and harder to clear.
  • 'char[] and String are for different purposes: mutability versus convenient text handling.'
  • For ordinary code, use the simplest conversion method that matches the needed slice of the array.

Course illustration
Course illustration

All Rights Reserved.