Write a program to Convert a character array to string
This is a Java program that demonstrates how to convert a character array to a string using the String class constructor.
- The program starts by declaring a string variable called str and a character array variable called chrArr containing the characters 'J', 'A', 'V', and 'A'.
- The program then creates a new empty string using the String class constructor with no arguments. It also declares the str variable to store the converted character array.
- Next, the program converts the character array to a string using the String class constructor that takes a character array as an argument. This constructor creates a new string that contains the characters from the character array.
- Finally, the program prints out the converted string using the println() method.
Source Code
import java.lang.*;
public class CharArray_String
{
public static void main(String []args)
{
String str="";
char [] chrArr= new char[]{'J','A','V','A'};
str= new String(chrArr); // convert char array to string
System.out.println("After Convert char Array to String : "+str);
}
}
Output
After Convert char Array to String : JAVA