0
0
JavaHow-ToBeginner · 2 min read

Java How to Convert Char Array to String Easily

You can convert a char array to a string in Java by using the constructor new String(charArray) or the method String.valueOf(charArray).
📋

Examples

Input['h', 'i']
Outputhi
Input['J', 'a', 'v', 'a']
OutputJava
Input[]
Output
🧠

How to Think About It

To convert a char array to a string, think of the char array as a list of letters you want to join together. Java provides simple ways to join these letters into one word or sentence by creating a new string from the array.
📐

Algorithm

1
Get the char array input.
2
Use the String constructor or String.valueOf method to create a string from the char array.
3
Return or print the resulting string.
💻

Code

java
public class CharArrayToString {
    public static void main(String[] args) {
        char[] chars = {'J', 'a', 'v', 'a'};
        String str = new String(chars);
        System.out.println(str);
    }
}
Output
Java
🔍

Dry Run

Let's trace the example char array ['J', 'a', 'v', 'a'] through the code

1

Input char array

chars = ['J', 'a', 'v', 'a']

2

Create string from char array

str = new String(chars) results in "Java"

3

Print the string

Output: Java

StepActionValue
1Input char array['J', 'a', 'v', 'a']
2Convert to String"Java"
3Print outputJava
💡

Why This Works

Step 1: Using String constructor

The new String(charArray) creates a new string by joining all characters in the array.

Step 2: Alternative method

The String.valueOf(charArray) method also converts the char array to a string in a similar way.

Step 3: Result

Both methods produce a string that contains all characters from the array in order.

🔄

Alternative Approaches

Using String.valueOf()
java
public class CharArrayToString {
    public static void main(String[] args) {
        char[] chars = {'H', 'i'};
        String str = String.valueOf(chars);
        System.out.println(str);
    }
}
This method is simple and clear, and often preferred for readability.
Using StringBuilder append
java
public class CharArrayToString {
    public static void main(String[] args) {
        char[] chars = {'B', 'y', 'e'};
        StringBuilder sb = new StringBuilder();
        for (char c : chars) {
            sb.append(c);
        }
        System.out.println(sb.toString());
    }
}
This approach is useful if you want to build the string step-by-step or modify it during creation.

Complexity: O(n) time, O(n) space

Time Complexity

Converting a char array to a string requires processing each character once, so it takes linear time proportional to the array length.

Space Complexity

A new string is created that stores all characters, so space used is proportional to the array size.

Which Approach is Fastest?

Using the String constructor or String.valueOf is equally efficient and simpler than manually appending characters.

ApproachTimeSpaceBest For
String constructorO(n)O(n)Simple direct conversion
String.valueOf()O(n)O(n)Readable and concise code
StringBuilder appendO(n)O(n)Building string with modifications
💡
Use new String(charArray) for a quick and direct conversion from char array to string.
⚠️
Trying to print the char array directly without conversion prints the array's memory address, not the characters.