Character Array in Java

Last Updated : 15 Jan 2026

Character Array is an array that holds character data types values. In Java programming, unlike C, a character array is different from a string array, and neither a string nor a character array can be terminated by the NULL character.

The Java language uses UTF-16 representation in a character array, string, and StringBuffer classes.

The character arrays are very helpful in Java. It is very efficient and faster. The data can be manipulated without any memory allocations.

In Java, Strings are immutable. It means that we cannot change their internal state once created. However, char arrays allow us to manipulate after the creation even data structures List and Set are also acceptable.

What is a character in Java?

In Java, the characters are primitive data types. The char keyword is used to declare the character types of variables. The default value of a char data type is '\u0000'. The character values are enclosed with a single quote. Its default size is 2 bytes.

The char data type can store the following values:

  • Any Alphabet (A-Z or a-z)
  • Numbers between 0 to 65,535 (Inclusive)
  • Special Characters (@, #, $, %, ^, &, *, (, ), ¢, £, ¥)
  • 16-bit Unicode Characters.

Common Use Cases

Character arrays find applications in various scenarios, including:

  • String Manipulation: Many string manipulation algorithms operate on character arrays due to their mutable nature.
  • Input/Output Handling: Character arrays are commonly used in reading and writing operations, especially when dealing with streams of character data.
  • Parsing and Tokenization: When parsing textual data or breaking it into tokens, character arrays are often used for efficient processing.
  • Performance Optimization: In performance-critical applications, using character arrays can offer better performance compared to string manipulation due to reduced memory overhead and mutable nature.

How to declare Character Arrays?

We can declare the character array using the char keyword with square brackets. The character array can be declared as follows:

We can place the square bracket at the end of the statement as well:

After the declaration, the next thing is initialization. Let's understand how to initialize the character array:

How to Initialize Character Array?

We can initialize the character array with an initial capacity. For example, to assign an instance with size 5, initialize it as follows:

The values will be assigned to this array as follows:

We can perform various useful operations such as sorting, looping, conversion to a string, and more on character array.

Loops in Character Array

We can use for loop to iterate through the values in a character array.

Consider the following example:

Example

Compile and Run

Output:

a
b
c
d
e

We can also iterate it as follows:

Example

Compile and Run

Output:

a
b
c
d
e

We observe that both the programs produce the same output. So we can iterate the character array using any of the above implementation methods.

Sorting a Character Array

The Arrays.sort() method is used to sort an array. Consider the following Java program that demonstrate the same.

To read more Arrays.sort() Method

Example

Compile and Run

Output:

[a, b, c, d, e]

In the above example, we see the array values are printed in sorted order. By default, it will sort in ascending order.

Finding the Length of a Character Array

We can count the length of an array by using the length property.

Consider the following example.

Example

Compile and Run

Output:

6

The above Java program finds the length of specified array.

How to Convert a String Array into Character Array?

We can easily convert a string array into a character array by using the String.toCharArray() method. It is the easiest method to convert a string field into a character field.

Consider the following example.

Example

Compile and Run

Output:

t
p
o
i
n
t
t
e
c
h

Additional Points on Character Arrays in Java

1. Concatenation: Character arrays can be concatenated using the + operator or using the System.arraycopy() method for more efficient concatenation.

Example

Compile and Run

Output:

Concatenated Array: [a, b, c, d, e, f]

2. Searching: We can search for a specific character or sequence of characters within a character array using various techniques such as linear search or more efficient algorithms like binary search for sorted arrays.

Example

Compile and Run

Output:

Character found in the array.

3. Subarrays: We can extract subarrays from a character array using the Arrays.copyOfRange() method or by manually copying elements.

Example

Compile and Run

Output:

Subarray: [b, c]

4. Conversion to Primitive Data Types:You can convert individual characters in a character array to their respective primitive data types using type casting.

Example

Compile and Run

Output:

Converted Value: 1

5. Performance Considerations: While character arrays offer mutability and efficiency, excessive manipulation can lead to performance overhead. It's essential to balance readability and performance when working with character arrays.

Conclusion

Character arrays in Java provide a flexible and efficient means of working with sequences of characters. Unlike strings, character arrays are mutable, allowing for direct manipulation of their contents. They find applications in various domains such as string processing algorithms, input/output handling, and data parsing. By understanding their initialization, manipulation, and associated operations, Java developers can leverage character arrays effectively to build robust and optimized applications.