Java String toCharArray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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
CallWhat 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

ParameterDescription
NonetoCharArray() takes no parameters.

Return Value

Return ValueDescription
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);
    }
}

How It Works

  1. One slot per char. The four characters of "Java" fill an array of length 4.
  2. Print it readably. Arrays.toString shows [J, a, v, a]; passing the array straight to println(Object) would print an identity string instead.
  3. 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));
    }
}

How It Works

  1. Strings cannot be sorted in place. They are immutable, so each word is first copied into its own array.
  2. Sort both copies. Arrays.sort orders the chars, and both words become e i l n s t.
  3. 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));
    }
}

How It Works

  1. Write into the copy. Setting chars[0] = 'J' turns the array into J a v a, which new String(chars) prints as "Java".
  2. The source is untouched. word still says "lava", because the array never shared storage with the string.
  3. 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.

Next: toLowerCase()

Convert every letter to lowercase (pass a Locale to be safe).

toLowerCase() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful