Char Array to String in Java: Four Conversion Methods
- new String(chars) and String.valueOf(chars) are the two most common approaches β both produce identical results for non-null arrays.
- String.valueOf(null) returns the string 'null' rather than null β use explicit null check if null propagation matters.
- new String(chars, offset, count) handles partial array conversion β useful when your char[] contains more data than you want.
char[] to String conversion comes up surprisingly often in real codebases. Security-conscious APIs (like Java's own KeyStore and JPasswordField) return passwords as char[] rather than String precisely because char arrays can be zeroed out after use, while String literals are interned and may linger in the heap. Understanding the conversion and knowing which method to use matters for correctness, not just convenience.
Four Methods for char[] to String Conversion
All four methods produce the same output for typical use cases. The differences matter at the margins: performance for large arrays, null-safety, and intent signalling.
package io.thecodeforge.strings; import java.util.Arrays; public class CharArrayConversionExample { public static void main(String[] args) { char[] chars = {'T', 'h', 'e', 'C', 'o', 'd', 'e', 'F', 'o', 'r', 'g', 'e'}; // Method 1: String constructor β most common and idiomatic String s1 = new String(chars); System.out.println("Constructor: " + s1); // Method 2: String.valueOf() β same implementation, cleaner intent String s2 = String.valueOf(chars); System.out.println("valueOf: " + s2); // Method 3: StringBuilder append β useful when building incrementally StringBuilder sb = new StringBuilder(); sb.append(chars); String s3 = sb.toString(); System.out.println("StringBuilder: " + s3); // Method 4: String.copyValueOf() β explicit copy semantics String s4 = String.copyValueOf(chars); System.out.println("copyValueOf: " + s4); // Partial range conversion β constructor with offset and count String partial = new String(chars, 3, 8); // offset=3, count=8 System.out.println("Partial [3,8]: " + partial); // CodeForg // Security use case: zero out the char array after conversion char[] password = {'s', 'e', 'c', 'r', 'e', 't'}; String passwordStr = new String(password); Arrays.fill(password, '\0'); // Overwrite sensitive data System.out.println("Password zeroed: " + Arrays.toString(password)); } }
valueOf: TheCodeForge
StringBuilder: TheCodeForge
copyValueOf: TheCodeForge
Partial [3,8]: CodeForg
Password zeroed: [\0, \0, \0, \0, \0, \0]
| Method | Syntax | Null-safe? | Use When |
|---|---|---|---|
| String constructor | new String(chars) | NPE on null | Default choice β clear intent |
| String.valueOf() | String.valueOf(chars) | Returns 'null' string | Null safety matters |
| StringBuilder.append() | sb.append(chars).toString() | No | Building incrementally |
| String.copyValueOf() | String.copyValueOf(chars) | NPE on null | Legacy code compatibility |
π― Key Takeaways
- new String(chars) and String.valueOf(chars) are the two most common approaches β both produce identical results for non-null arrays.
- String.valueOf(null) returns the string 'null' rather than null β use explicit null check if null propagation matters.
- new String(chars, offset, count) handles partial array conversion β useful when your char[] contains more data than you want.
- Always zero out char[] password arrays with Arrays.fill(password, '\0') after use β this is why security APIs return char[] instead of String.
β Common Mistakes to Avoid
- βUsing Arrays.toString(chars) β this produces '[T, h, e, C, o, d, e, F, o, r, g, e]' with brackets and commas, not the string you want.
- βNot zeroing the char array after converting a password β if your code handles passwords as char[], call Arrays.fill(password, '\0') after conversion to minimise exposure time.
- βCalling String.valueOf(null) expecting null back β it returns the literal string 'null', not null. Check for null before converting if your downstream code needs to handle null.
Interview Questions on This Topic
- QWhat are the different ways to convert a char array to a String in Java?
- QWhy do some security APIs return char[] instead of String for passwords?
Frequently Asked Questions
How do I convert a char array to a String in Java?
The most common approach is new String(chars) or String.valueOf(chars) β both produce the same result. Use String.valueOf() if you need null-safe behaviour (it returns the literal string 'null' for a null input rather than throwing NullPointerException).
Why does Arrays.toString(chars) not work for char array to String conversion?
Arrays.toString() formats the array as a string representation: '[T, h, e, C, o, d, e]' with brackets and commas between each character. Use new String(chars) or String.valueOf(chars) to concatenate the characters without any formatting.
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.