Java Programs›Strings›Convert String to char Array

Convert String to char Array in Java

beginner·  Strings  ·  String

Problem

A Java String is immutable, so its characters can't be changed in place; copying them into a char array gives you a mutable sequence you can edit freely.

Given a word, convert it to an array of characters, change the array, and show that the original string is unaffected.

Input
"Coffee"
Output
[C, o, f, f, e, e]

Java Program

Java
import java.util.Arrays; public class StringToCharArray { public static void main(String[] args) { String word = "Coffee"; char[] letters = word.toCharArray(); // a fresh copy, independent of the string System.out.println(Arrays.toString(letters)); letters[0] = 'c'; // editing the array leaves the original string untouched System.out.println("Array now: " + new String(letters)); System.out.println("String still: " + word); } }

Output

[C, o, f, f, e, e] Array now: coffee String still: Coffee

Core Logic

toCharArray returns a brand-new array filled with the string's characters, so changes to the array never reach the string.

How It Works
  1. 1word.toCharArray() creates a char[] of length 6 holding each character.
  2. 2Arrays.toString(letters) prints the array in a readable bracketed form.
  3. 3letters[0] = 'c' changes only the array's first element.
  4. 4new String(letters) turns the edited array back into a string, while word still prints as before.
After the edit, the array reads coffee but word is still Coffee.
💡

Key Point: Converting to a char array is the standard first step for in-place string algorithms such as reversing with two pointers or sorting the letters of a word.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Every one of the n characters is copied into a new array of the same length.

Key Concepts

toCharArray()Arrays.toString()new String(char[])immutability

Approach 2: Manual Copy with charAt()

Java
import java.util.Arrays; public class StringToCharArrayManual { public static void main(String[] args) { String word = "Coffee"; char[] letters = new char[word.length()]; for (int i = 0; i < word.length(); i++) { letters[i] = word.charAt(i); // copy one character at a time } System.out.println(Arrays.toString(letters)); } }

Output

[C, o, f, f, e, e]

Core Logic

The same array can be filled by hand: allocate it to the string's length and copy characters one index at a time.

How It Works
  1. 1new char[word.length()] creates an empty array of the right size.
  2. 2The loop runs i from 0 to length - 1.
  3. 3letters[i] = word.charAt(i) copies each character to the same position.
Index 2 of "Coffee" is 'f', so letters[2] becomes 'f'.
💡

Key Point: This version is useful when you only want part of the string, or want to transform characters as you copy — otherwise toCharArray is simpler.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: One pass copies n characters into an n-length array.

Key Concepts

charAt()for looparray allocation

Interview Questions

Does changing the char array change the original string?

No. toCharArray() returns a new copy, so edits to the array never affect the immutable String.

How do you turn a char array back into a string?

Use new String(chars) or String.valueOf(chars). Calling chars.toString() prints the array's type and hash code, not its contents.

Learn the Concept

Related Programs