Home » Java string programs

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.