HOWTO · Java
How to Convert a String to Char in Java
Learn when to use Java's charAt() for one UTF-16 code unit or toCharArray() for a full string, how indexing works, and why getBytes() encodes bytes rather than converting a string to characters.
On this page
Java’s String type represents text as a sequence of UTF-16 code units. To retrieve one unit, use charAt(index). To get an array of the string’s units, use toCharArray(). These are different tasks: a Java char is one 16-bit code unit, and it does not always represent a complete Unicode character.
The examples below are Java source examples; no execution output is claimed here.
Get one char with charAt()
charAt() returns the code unit at a zero-based index. The first position is 0, and the final valid position is string.length() - 1. The index must be in that range.
Run this example with a Java compiler to retrieve the first unit:
public class StringToCharExample {
public static void main(String[] args) {
String word = "Hello";
char first = word.charAt(0);
System.out.println(first);
}
}
An index outside that range causes a StringIndexOutOfBoundsException; check the string’s length when the index comes from input or calculation.
Use this approach when an operation needs one position, such as checking the first letter. It does not convert the entire string into a single char; a char can hold only one UTF-16 code unit.
Convert a string to a char[] with toCharArray()
Call toCharArray() when you need an array containing the string’s UTF-16 code units. The returned array has one element per code unit, so its length matches the string’s length(). You can access or change elements in the array without changing the original string.
import java.util.Arrays;
public class StringToCharArrayExample {
public static void main(String[] args) {
String word = "Java";
char[] units = word.toCharArray();
System.out.println(Arrays.toString(units));
System.out.println(units[0]);
}
}
Choose toCharArray() to process each code unit or to pass a mutable array to code that requires one. Choose charAt() when you only need a particular position; it avoids creating an array.
Understand char and Unicode code points
Many common characters fit in one UTF-16 code unit, but supplementary Unicode code points are encoded as a pair of code units. For such a character, charAt() at one position returns only one half of the pair, and toCharArray() stores the two units separately. This is why “character” can mean different things: a code unit, a Unicode code point, or a user-perceived symbol such as an emoji with modifiers.
If your task must process complete Unicode code points, iterating through char values or array elements alone is insufficient. Use an approach designed to read code points as a unit. Do not assume that the number of visible symbols equals the string’s length().
Why getBytes() is not a string-to-char conversion
getBytes() encodes a string using a character set and returns bytes. A byte is not a Java char, and casting each encoded byte to char does not generally recover the original text. Encodings may use multiple bytes for a character, and the result depends on the charset. Use charAt() for one UTF-16 code unit or toCharArray() for the full sequence of code units; use byte encoding when the task requires data in a specified charset.