Home
Map
char ArrayUse char arrays to store characters and create Strings. Measure char array performance.
Java
This page was last reviewed on Jan 25, 2024.
Char arrays. In Java, Strings are immutable: they cannot be changed in place or added to. With char arrays, we manipulate character buffers.
Shows a char array
Array notes. Char arrays are faster—we can change text data without allocations. With the String constructor, we can convert back into a string.
Array
Convert char Array
Simple example. To begin, char arrays support the array initializer syntax like other arrays. Here we create a new array with 3 elements.
Tip Int values, which represent chars in ASCII, can also be used in a char array initializer.
Shows a char array
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
Complex example. This code is more complex. We create a 26-element array and then, in a for-loop, initialize all letters to the lowercase alphabet.
Detail To convert a char array to a String, we use the String constructor. An optional start index and count parameter can be used.
Constructor
Here We test the result 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 elements. Chars can be represented as numbers. We can create a char array with numbers like 97, 98 and 99 which in ASCII stand for "abc."
Note To the compiler, 97 is the same as "a" but using "a" is more readable for humans.
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
Benchmark, char array. We 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.
Version 1 This version of the code uses a char array to build up a buffer of 10 characters. It converts the buffer to a string.
Version 2 This code uses the StringBuilder type to append 10 chars and then calls toString.
Result If a program creates strings of known length, using a 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 25, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.