In Java, toCharArray() returns a new char[] whose length equals the string's length() and whose elements are the string's characters in order.
Use it when you need to modify, sort, or index characters as an array, for example to check anagrams or reverse text. Every call allocates a fresh array, so changing it never affects the immutable string.
toCharArray() is an instance method of java.lang.String, available since JDK 1.0. Call it on a string, as in text.toCharArray(); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed.
Syntax
Call it with no arguments and keep the array.
java
char[] letters = word.toCharArray();
Arrays.sort(letters); // sorting the copy is fine
String sorted = new String(letters); // back to a String
Call
What it does
"Java".toCharArray()
{'J', 'a', 'v', 'a'}.
"".toCharArray()
An empty array of length 0.
new String(s.toCharArray())
A new String with the same text.
Each char is one UTF-16 unit, so an emoji occupies two array slots. To work with whole Unicode characters, use codePoints() instead.
Library declarations
java
public char[] toCharArray();
Parameters
Parameter
Description
None
toCharArray() takes no parameters.
Return Value
Return Value
Description
char[]
A newly allocated array containing a copy of the characters.
Java Examples
Example 1 — Turn a String into a char Array
Print the array properly and count the uppercase letters in it.
java
import java.util.Arrays;
public class ToCharArrayBasic {
public static void main(String[] args) {
char[] letters = "Java".toCharArray();
System.out.println(Arrays.toString(letters));
System.out.println("length: " + letters.length);
int upper = 0;
for (char c : letters) {
if (Character.isUpperCase(c)) {
upper++;
}
}
System.out.println("uppercase letters: " + upper);
}
}
Output
[J, a, v, a]
length: 4
uppercase letters: 1
How It Works
One slot per char. The four characters of "Java" fill an array of length 4.
Print it readably.Arrays.toString shows [J, a, v, a]; passing the array straight to println(Object) would print an identity string instead.
Loop like any array. The for-each loop tests every element, and only 'J' is uppercase.
Example 2 — Sort the Characters to Detect Anagrams
Two words are anagrams when their sorted letters are identical.
java
import java.util.Arrays;
public class ToCharArrayAnagram {
public static void main(String[] args) {
char[] a = "listen".toCharArray();
char[] b = "silent".toCharArray();
Arrays.sort(a);
Arrays.sort(b);
System.out.println(new String(a));
System.out.println(new String(b));
System.out.println("anagrams? " + Arrays.equals(a, b));
}
}
Output
eilnst
eilnst
anagrams? true
How It Works
Strings cannot be sorted in place. They are immutable, so each word is first copied into its own array.
Sort both copies.Arrays.sort orders the chars, and both words become e i l n s t.
Compare element by element.Arrays.equals finds the arrays identical, so the words use exactly the same letters.
Example 3 — Editing the Array Leaves the String Alone
Change the first letter of the array and compare with the original string.
java
public class ToCharArrayCopy {
public static void main(String[] args) {
String word = "lava";
char[] chars = word.toCharArray();
chars[0] = 'J';
System.out.println("array as text: " + new String(chars));
System.out.println("original: " + word);
char[] again = word.toCharArray();
System.out.println("fresh array? " + (again != chars));
}
}
Output
array as text: Java
original: lava
fresh array? true
How It Works
Write into the copy. Setting chars[0] = 'J' turns the array into J a v a, which new String(chars) prints as "Java".
The source is untouched.word still says "lava", because the array never shared storage with the string.
A new array every call. Calling toCharArray() again returns a different array object, so again != chars is true.
Frequently Asked Questions
A new char array with the same length and characters as the string.
No. The array is a copy. Strings are immutable, so the original string never changes.
Use new String(chars) or String.valueOf(chars). Both copy the characters into a new String.
Use Arrays.toString(chars) for [a, b, c] style output, or new String(chars) to print the characters as text.
Each emoji outside the Basic Multilingual Plane becomes two chars (a surrogate pair). Use codePoints() to work with whole characters.