Java toCharArray Method

The Java toCharArray String Method is helpful to convert the user-specified string into Character Array (Char Array). In this article, we show how to use this Java String toCharArray to convert the string of words into char array with an example.

The syntax of the String toCharArray in Java Programming language is as shown below. The following Java toCharArray method will not accept any parameters. However, it converts the string object into Character Array.

public char[] toCharArray(); 

//In order to use in program

Java toCharArray Method Example

This program uses the String tochararray method to convert the user specified string object into Character Array. First, we declared string variable with sample text.

The following toCharArray String Method statement in this program convert the every character present in the previously declared str into char Array.

Next, we used the Foreach Loop to iterate every character present in the newly created Character Array Ch. And it assigns each character to char cr.

Within the Java Foreach loop, we used the System.out.println statement. It will print the characters as an output.

package StringFunctions;

public class ToCharArrayMethod {
	public static void main(String[] args) {
		String str = "Tutorial GateWay";
		char[] ch = str.toCharArray();
		for (char cr: ch) {
			System.out.println("Character Array Element = " + cr); 
Java toCharArray 1