Write a program to create a character array containing the contents of a string
This Java program converts a given String to a char array using the toCharArray() method and then prints the resulting array.
- The program defines a public class named "Create_CharArray" with a main method that takes no arguments. The main method declares a String variable str with the value "Java Exercises.".
- Then, the program uses the toCharArray() method of the String class to convert str to a char array and stores the result in the char array variable a.
- Finally, the program prints out the resulting char array a using the System.out.println method. When you print a char array directly using System.out.println, it prints the contents of the array as a String.
Source Code
public class Create_CharArray
{
public static void main(String[] args)
{
String str = "Java Exercises.";
char[] a = str.toCharArray();
System.out.println(a);
}
}
Output
Java Exercises.