Homeβ€Ί Javaβ€Ί Char Array to String in Java: Four Conversion Methods

Char Array to String in Java: Four Conversion Methods

Where developers are forged. Β· Structured learning Β· Free forever.
πŸ“ Part of: Strings β†’ Topic 11 of 15
Convert a char array to String in Java using String constructor, String.
πŸ§‘β€πŸ’» Beginner-friendly β€” no prior Java experience needed
In this tutorial, you'll learn:
  • 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.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
⚑ Quick Answer
A char array in Java is a sequence of individual characters. A String is an immutable object wrapping character data. Converting between them is a basic operation you'll need constantly β€” parsing input, processing text from legacy APIs, working with cryptography code that deals in char[] rather than String for security reasons.

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.

CharArrayConversionExample.java Β· JAVA
1234567891011121314151617181920212223242526272829303132333435363738
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));
    }
}
β–Ά Output
Constructor: TheCodeForge
valueOf: TheCodeForge
StringBuilder: TheCodeForge
copyValueOf: TheCodeForge
Partial [3,8]: CodeForg
Password zeroed: [\0, \0, \0, \0, \0, \0]
MethodSyntaxNull-safe?Use When
String constructornew String(chars)NPE on nullDefault choice β€” clear intent
String.valueOf()String.valueOf(chars)Returns 'null' stringNull safety matters
StringBuilder.append()sb.append(chars).toString()NoBuilding incrementally
String.copyValueOf()String.copyValueOf(chars)NPE on nullLegacy 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.

πŸ”₯
Naren Founder & Author

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.

← PreviousCharacter Class in JavaNext β†’List to Comma Separated String in Java
Forged with πŸ”₯ at TheCodeForge.io β€” Where Developers Are Forged