Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Char Array to String: Conversion Methods

Learn how to convert a char array to a String in Java using new String(char[]), String.valueOf(), and StringBuilder, with notes on performance, mutability, and common pitfalls.

~4 min readJava
#Java#String conversion#char array#StringBuilder
Diagram showing conversion of a character array to a Java String object.

Converting a char[] to a String in Java is a common task with several valid approaches. The core conversion uses the String constructor, but static helpers and StringBuilder are useful in different contexts. This article covers those methods, their behavior, and the pitfalls to avoid.

The Core Conversion: new String(char[])

The most direct way to convert a char[] to a String in Java is to use the String constructor that accepts a character array. The expression new String(charArray) creates a new String object whose content is a copy of the characters in the array. This is the simplest and most readable approach when you have a complete array and want an independent string.

java
char[] chars = {'J', 'a', 'v', 'a'}; String text = new String(chars); System.out.println(text); // Java

The constructor also has overloads that accept an offset and length, which is useful when you need to convert only a portion of the array. For example, new String(chars, 1, 3) would produce "ava" from the array above. This overload avoids creating a temporary subarray and is efficient when you already know the boundaries.

Using String.valueOf(char[])

String.valueOf(char[]) is a static method that performs the same conversion as the String constructor. Unlike String.valueOf(Object), however, the char[] overload is not null-safe: passing a null array throws a NullPointerException.

java
char[] data = null; String result = String.valueOf(data); // throws NullPointerException

If you need the literal string "null" when the array is null, check for null first:

java
char[] data = null; String result = data == null ? "null" : new String(data);

For most conversion scenarios where the array is known to be non-null, either new String(char[]) or String.valueOf(char[]) works equally well.

Converting with StringBuilder for Chained Operations

If you are already building a string incrementally, StringBuilder offers an append(char[]) method that can be used as part of a larger construction. This is not a direct one-step conversion, but it is relevant when the char array is one of several components in the final string.

java
char[] first = {'H', 'e', 'l', 'l', 'o'}; StringBuilder sb = new StringBuilder(); sb.append(first).append(" World"); String message = sb.toString();

The append(char[]) method copies the characters into the builder's internal buffer. This approach is preferable when you are concatenating multiple pieces and want to avoid creating intermediate String objects. It also allows you to append a subrange using append(char[], int offset, int len).

Mutability: Why the Source Array Matters

A String is immutable in Java, but the char[] you convert from is not. When you use new String(charArray), the String constructor makes a defensive copy of the array's contents. This means subsequent modifications to the original array do not affect the String. This is the desired behavior in most cases, especially when the array is reused or passed around.

The conversion is always a snapshot. If you need a mutable character sequence and want to produce an immutable String later, use StringBuilder (or StringBuffer) as the working buffer and call toString() when you need the snapshot. There is no mutable subclass of String, so you cannot create a String object whose contents track changes in a source array.

Performance and Memory Considerations

The conversion process always involves copying the characters from the array into the new String's internal storage (or into the StringBuilder buffer). This is an O(n) operation in terms of time and memory. For small arrays, the overhead is negligible, but for very large arrays, you should be aware that a temporary copy is made.

If you convert a large array and then discard the array, memory usage is effectively doubled during the conversion. In memory-constrained environments, consider using a StringBuilder to build the data incrementally or releasing the source array once the String has been created. For most applications, the standard conversion methods are well optimized. There is no way to create a String that directly wraps an existing char[] without copying, because that would break immutability guarantees.

Choosing the Right Method for Your Use Case

The approach you choose depends on your specific context:

  • Use new String(char[]) when you have a complete array and want a clean, explicit conversion.
  • Use String.valueOf(char[]) when you prefer the static method form, but remember it also throws on null input.
  • Use StringBuilder when you are building a larger string and the char array is just one part of it.

If you are converting a subrange, the offset-based constructors and append methods are more efficient than creating a subarray manually. Prefer the built-in overloads over manual copying.

Common Pitfalls When Converting Char Arrays

One common mistake is assuming that the String will reflect changes to the original array. As noted, the conversion copies the data, so this is not the case. Another pitfall is using toString() directly on the array, which returns something like [C@15db9742 (the class name and hash code) rather than the character content. Arrays do not override Object.toString().

java
char[] chars = {'a', 'b'}; System.out.println(chars.toString()); // e.g. [C@15db9742 System.out.println(new String(chars)); // ab

Always use the explicit conversion methods. Also be mindful of the offset and length parameters: if you specify an invalid range, an IndexOutOfBoundsException will be thrown. Validate your indices when the array length is not guaranteed.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.