How to convert char array to String

String(char[])

String class accepts char array in its constructor and converts it into string.

package com.techstackjournal;

public class CharArrayToString {

	public static void main(String[] args) {

	     char data[] = {'J', 'a', 'v', 'a'};
	     String str = new String(data);
	     System.out.println(str);

	}

}

Output

Java

String.valueOf(char[])

String class provides valueOf method which accepts char array and returns a String object.

	char data[] = { 'J', 'a', 'v', 'a' };
	System.out.println(String.valueOf(data));

StringBuilder.append(char[])

StringBuilder class provides an append method that can accept a char array. This method reads all characters and appends them to the buffer. We can then call the StringBuffer.toString() method to get the String object.

	char data[] = { 'J', 'a', 'v', 'a' };
	StringBuffer strBfr = new StringBuffer();
	strBfr.append(data);
	System.out.println(strBfr);

StringBuffer.append(char[])

Similar to StringBuilder, the StringBuffer class also provides an append method that accepts a char array to the final StringBuilder object. Then we can get a String object by calling StringBuilder.toString() method.

	char data[] = { 'J', 'a', 'v', 'a' };
	StringBuilder strBldr = new StringBuilder();
	strBldr.append(data);
	System.out.println(strBldr);
See also  How to Reverse an Integer in Java