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 method will not accept any parameters. However, it converts the string object into Character Array.

public char[] toCharArray(); // It will return String 

//In order to use in program
String_Object.toCharArray()

Java toCharArray Method Example

This program uses the String tochararray method to convert the user specified string object into Character Array.

// String.toCharArray Java Example 
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

The following toCharArray String Method statement in this program convert the every character present in the previously declared String into Character Array (char Array)

char[] ch = str.toCharArray();

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.

for (char cr: ch) {

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

System.out.println("Character Array Element = " + cr);