Java Char Array

Use char arrays to store characters and create Strings. Measure char array performance.

Char arrays. Strings are immutable: they cannot be changed in place or added to. With char arrays, we manipulate character buffers.ArrayCharacter

Performance advantages. Char arrays are faster—we can change text data without allocations. With the String constructor, we can convert back into a string.Char Array, String

Example program. Here we use a char array. We create a 26-element array and then, in a for-loop, initialize all letters to the lowercase alphabet.

String: To convert a char array to a String, we use the String constructor. An optional start index and count parameter can be used.


Here: We test the result string. We show it is a String that contains the letters "abc" at its start.

Java program that uses char array 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); } } Output true 26 abcdefghijklmnopqrstuvwxyz

Initializer statement. Char arrays support the array initializer syntax like other arrays. Here we create a new array with seven elements.

Tip: Int values, which represent chars in ASCII, can also be used in a char array initializer.

Java program that uses char array initializer public class Program { public static void main(String[] args) { // Use array initializer syntax. char[] array = { 'a', 'r', 't', 'i', 's', 't' }; // Add only the first three characters to a new string. String result = new String(array, 0, 3); System.out.println(result); } } Output art

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.

Java program that uses char array, ints 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); } } Output 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.
Java program that uses toCharArray 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); } } } Output 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.

Java program that benchmarks char array, StringBuilder 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); } } Output 38 ms: char[] array 81 ms: StringBuilder append

A summary. 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
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to