Convert Character Array to String in Java
In this tutorial, we’ll cover multiple programs to convert a character array to a String using Java code.
Reading time 5 minutes
Using String constructor
String class in Java provides a constructor which accepts a character array, will create a string object, and stores the array of characters in it.
package stringPrograms;
public class ArrayToString {
public static void main(String[] args) {
char chars[] = {'c','o','d','e','d','a','i','l','y'}; //character array
String str = new String(chars);
System.out.println(str);
}
}
Using StringBuilder class
StringBuilder class in Java can append character to it’s object. After appending all the characters can be converted to String object using toString() method.
public class ArrayToString {
public static void main(String args[]) {
// Defining character array
char [] chars = {'c','o','d','e','d','a','i','l','y'};
StringBuilder sb = new StringBuilder();
//Using append() method to create a string
for (int i = 0; i < chars.length; i++) {
chars.append(a[i]);
}
System.out.println(sb.toString());
}
}Using valueOf() method of String class
This method takes the character array as input and converts the character array to a format where the entire value of the characters present in the array is displayed. In short, the array is converted to a String.
public class ArrayToString {
public static void main(String args[]) {
// Defining character array
char [] chars = {'c','o','d','e','d','a','i','l','y'};
// Using valueOf method to convert
String str = String.valueOf(chars);
System.out.println(str);
}
}













