×

Java Programs

Java Practice

How to convert a character array to string in Java?

Learn: How to convert a given character array to string? Here we will take a character array and string object, and then character array will be converted and assigned to the string object.

Given a character array and we have to convert it to string.

In this example, we are taking a character array chrArr which is initializing with string character by character. And then, converting character array to string using new String(chrArr) and assigning to the string object str.

Consider the program:

import java.lang.*;

public class ConvertCharArrayToStringPrg
{
	public static void main(String []args)
	{
		// declare String object
		String str="";     
		// declare character array
		char [] chrArr= new char[]{'H','e','l','l','o'};   

		 // convert char array to string
		str= new String(chrArr);  

		//print string
		System.out.println("str : "+str);
	}
}

Output

str: Hello

Java String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.