String
class constructor that takes a char[]
as its argument. Here's an example :class CharArrayToString
{
public static void main(String args[])
{
// Method 1: Using String object
char[] ch = {'H', 'e', 'l', 'l', 'o'};
String str = new String(ch);
System.out.println(str);
// Method 2: Using valueOf method
String str2 = String.valueOf(ch);
System.out.println(str2);
}
}
Hello
Hello
char[]
array called charArray
with the characters 'H', 'e', 'l', 'l', 'o'
. We then create a new String
object called str
by passing charArray
to the String
constructor. Finally, we print str
to the console using System.out.println()
.