Javatpoint Logo
Javatpoint Logo

Character Array in Java

Character Array in Java 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 NUL character.

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

The Character arrays are very advantageous in Java. They are very efficient and faster. Also, the data can be manipulated without any allocations.

Java Strings are immutable means we can not change their internal state once they are 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 and methods. The default value of a char data type '\u0000'. The character values are enclosed with a single quote. Its default size is 2 bytes.

The char data type can sore the following values:

  • Any alphabet
  • Numbers between 0 to 65,535 ( Inclusive)
  • special characters (@, #, $, %, ^, &, *, (, ), ¢, £, ¥)
  • 16-bit Unicode characters.

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 assign to this array as follows:

We can perform many useful operations such as sorting, looping, conversion to a string, and more on character array. Let's understand them:

Loops in Character Array

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

Consider the below example:

CharArrayDemo.java:

Output:

a
b
c
d
e

We can also iterate it as follows:

CharArrayDemo1.java:

Output:

a
b
c
d
e

From the above examples, both programs are producing the same output. So we can iterate the character array using any of the above implementation methods.

Let's understand how to sort a character array:

Sorting a Character Array

The Arrays.sort() method is used to sort an array. Consider the below syntax of Array.sort() method:

Consider the below example:

CharArrayDemo2.java:

Output:

[a, b, c, d, e]

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

Length of a Character Array

We can count the length of an array by using the below syntax:

Consider the below example:

CharArrayDemo3.java:

Output:

6

From the above example, we can see the array length is displayed.

How to Convert a String Array into Character Array

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

Consider the below example:

Output:

J
a
v
a
T
P
o
i
n
t

From the above example, a string array is converted into a character array.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA