How To Convert Char Array To String In Java?

This tutorial is simple example for converting a character array into a simple string object. In general, there are two ways to convert the character array to string object, First approach is to pass the array into the string constructor and second approach is to using the String.valueOf(). Lets look at the example.


public class CharArrayToStringExample {
	public static void main(String[] args) {

		char[] charArray = new char[] { 'a', 'b', 'c', 'd', '1', '2' };

		//Convert using String constructor
		String str1 = new String(charArray);
		System.out.println("String Constructor : " + str1);

		//Convert using String.valueOf method
		String str2;
		str2 = String.valueOf(charArray);
		System.out.println("String.valueOf() method : " + str2);



String Constructor : abcd12
String.valueOf() method : abcd12

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!