w3resource logo


Introduction to Java Programming

Java String: toCharArray() Method

Secondary Nav

public char[] toCharArray()

The toCharArray() method converts a given string to a new character array.

Java Platform: Java SE 8

Syntax :

toCharArray()

Return Value : a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

Return Value Type:

Example : Java String toCharArray() Method

The following example shows the usage of java String() method.

public class Example {

public static void main(String[] args)
    {
        String str = "Python Exercises.";

        // Convert the above string to a char array.
char[] arr = str.toCharArray();
    System.out.println();
        // Display the contents of the char array.
System.out.println(arr);
System.out.println();
    }
}

Output:

Python Exercises.

Java Code Editor:



Join our Question Answer community to learn and share your programming knowledge.