How to convert a char array to a string in Java

There are multiple ways to convert a character array (char[]) to a string in Java. You can use the string constructor, the String.valueOf() method and Streams API to convert a sequence of characters to a string object.

Convert char[] to a string using the String constructor

The easiest way to convert a character array to a string is using the string constructor e.g. new String(char[]).

The String class in Java provides an overloaded constructor that accepts a char array as an argument. Here is an example:

// Declare char array
char[] chars = {'M', 'a', 'n', 'g', 'o'};

// Convert the char array to a string
String str = new String(chars);

System.out.println(str);
// Mango

Convert char[] to a string using String.valueOf()

The String.valueOf() method is yet another simple way to convert a char[] to a string in Java. It returns the string representation of the passed argument, as shown below:

// Declare char array
char[] chars = {'M', 'a', 'n', 'g', 'o'};

// Convert the char array to a string
String str = String.valueOf(chars);

System.out.println(str);
// Mango

Convert char[] to a string using Arrays.stream()

Java 8 provides the Arrays.stream() method to create a stream using an array of characters. Then, you can use the Collectors.joining() method to convert the stream into a string object:

// Declare char array
Character[] chars = {'M', 'a', 'n', 'g', 'o'};

// Convert the char array to a string
String str = Arrays.stream(chars)
        .map(String::valueOf)
        .collect(Collectors.joining());

System.out.println(str);
// Mango

Read this article if you want to convert a string into a character array in Java.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.