Char
arraysIn Java, Strings are immutable: they cannot be changed in place or added to. With char
arrays, we manipulate character buffers.
Char
arrays are faster—we can change text data without allocations. With the String
constructor, we can convert back into a string
.
To begin, char
arrays support the array initializer syntax like other arrays. Here we create a new array with 3 elements.
Int
values, which represent chars in ASCII, can also be used in a char
array initializer.public class Program { public static void main(String[] args) { // Use array initializer syntax. char[] array = { 'a', 'b', 'c' }; // Add all characters to a new string. String result = new String(array, 0, array.length); System.out.println(result); } }abc
This code is more complex. We create a 26-element array and then, in a for
-loop, initialize all letters to the lowercase alphabet.
char
array to a String
, we use the String
constructor. An optional start index and count parameter can be used.string
. We show it is a String
that contains the letters "abc" at its start.public class Program { public static void main(String[] args) { // Create a char array of 26 characters. // ... Add all letters to it. char[] array = new char[26]; int index = 0; for (char c = 'a'; c <= 'z'; c++) { array[index++] = c; } String result = new String(array); // Convert to a string. // ... Display parts of our new string. System.out.println(result.startsWith("abc")); System.out.println(result.length()); System.out.println(result); } }true 26 abcdefghijklmnopqrstuvwxyz
Int
elementsChars can be represented as numbers. We can create a char
array with numbers like 97, 98 and 99 which in ASCII stand for "abc."
public class Program { public static void main(String[] args) { // Numbers can be stored in a char array. // ... These indicate a char based on ASCII. char[] values = new char[3]; values[0] = 97; values[1] = 98; values[2] = 99; System.out.println(values); // We can specify letters as well. char[] values2 = { 'a', 'b', 'c' }; System.out.println(values2); } }abc abc
ToCharArray
This method converts a String
into a char
array. It is the easiest way to get a char
array filled with the characters in a string
.
public class Program { public static void main(String[] args) { String value = "cat"; // Convert string to a char array. char[] array = value.toCharArray(); array[0] = 'h'; // Loop over chars in the array. for (char c : array) { System.out.println(c); } } }h a t
char
arrayWe can replace a StringBuilder
with a char
array in some programs. In this test, using a char
array is about twice as fast as a StringBuilder
.
char
array to build up a buffer of 10 characters. It converts the buffer to a string
.StringBuilder
type to append 10 chars and then calls toString
.char
array is a worthwhile optimization.import java.lang.StringBuilder; public class Program { public static void main(String[] args) { long t1 = System.currentTimeMillis(); // Version 1: create string from a char array. for (int i = 0; i < 1000000; i++) { char[] array = new char[10]; for (int v = 0; v < 10; v++) { array[v] = '?'; } String result = new String(array); } long t2 = System.currentTimeMillis(); // Version 2: create string from a StringBuilder. for (int i = 0; i < 1000000; i++) { StringBuilder builder = new StringBuilder(); for (int v = 0; v < 10; v++) { builder.append('?'); } String result = builder.toString(); } long t3 = System.currentTimeMillis(); // ... Benchmark timings. System.out.println(t2 - t1); System.out.println(t3 - t2); } }38 ms: char[] array 81 ms: StringBuilder append
With char
arrays, we manipulate text in a lower-level way. Usually Strings are preferable for data that is used in a program, but char
arrays offer a mutable approach.