Char Array In Java: Everything You need To Know About Character Arrays

Last updated on Jul 05,2024 313.8K Views

Char Array In Java: Everything You need To Know About Character Arrays

edureka.co

A good programming language should be adept to handle all data types. Lot of the data processed these days, also has characters in it. Java which is one of the best programming languages makes use of char array to hold data. In this article we would exploring everything that is there to know about them.

Following pointers will be discussed in this article,

Let us start with a quick introduction to Char array,

Char Arrays are highly advantageous. It is a noted fact that Strings are immutable i.e. their internal state cannot be changed after creation. However, with char arrays, character buffers can be manipulated. While the data structures List and Set are also acceptable, arrays prove to be simplistic and efficient. Furthermore, Char arrays are faster, as data can be manipulated without any allocations.

Java Full Course – 10 Hours | Java Full Course for Beginners | Java Tutorial for Beginners | Edureka

🔥𝐄𝐝𝐮𝐫𝐞𝐤𝐚 𝐉𝐚𝐯𝐚 𝐂𝐨𝐮𝐫𝐬𝐞 𝐓𝐫𝐚𝐢𝐧𝐢𝐧𝐠: https://www.edureka.co/java-j2ee-training-course (Use code “𝐘𝐎𝐔𝐓𝐔𝐁𝐄𝟐𝟎”)
This Edureka Java Full Course will help you understand the various fundamentals of Java programm…

Let us start this article on Char Array In Java, by understanding how to declare arrays in Java. though start with Java installation. If you don’t have it.

Declaring Char Array

Declaration of a char array can be done by using square brackets:

char[] JavaCharArray;

The square brackets can be placed at the end as well.

char JavaCharArray[];

The next step is to initialize these arrays

Initializing Char Array

A char array can be initialized by conferring to it a default size.

char[] JavaCharArray = new char[4];

This assigns to it an instance with size 4.

We use the following code to assign values to our char array:


char[] JavaCharArray = new char[5];
JavaCharArray[0] = 'r';
JavaCharArray[1] = 's';
JavaCharArray[2] = 't';
JavaCharArray[3] = 'u';

Loops play a very important role when it comes to avoiding repetitive code, this is the next bit of this Char Array in Java article,

Loops In Char Array

To iterate through every value present in the array, we make use of for loop.


char[] JavaCharArray = {'r', 's', 't', 'u', 'v'};
for (char c:JavaCharArray) {
System.out.println(c);
}

Output:

r

s

t

u

v

We can also implement the following method:


char[] JavaCharArray = {'r', 's', 't', 'u', 'v'};
for (int i=0; i<JavaCharArray.length; i++) {
System.out.println(JavaCharArray[i]);
}

This produces the same output as the previous code.

Let us see how to find out the length of Char Array

Length Of Char Array

The length of the character array can be determined as follows:


char[] JavaCharArray = {'r', 's', 't', 'u', 'v'};
System.out.println(JavaCharArray.length);

Output:

5

We can even sort char array, let us checkout how,

Sorting A Char Array

To sort the arrays, we can implement Arrays.sort() as shown below:


char[] JavaCharArray = {'r', 't', 'u', 's', 'v'};
Arrays.sort(JavaCharArray);
System.out.println(Arrays.toString(JavaCharArray));

Ouput:

[r, s, t, u, v]

The final bit of this Char Array in Java will talk about following pointers,

Converting A String Array Into Char Array

The following code converts a string into a char array.


public class Main {
public static void main(String[] args) {
String value = "hello";
//Convert string to a char array.
char[] array = value.toCharArray();
array[0] = 'j';
//Loop over chars in the array.
for(char c : array) {
System.out.println(c);
}
}
}

Output:

j

e

l

l

o

 

While Strings are usually preferred, char arrays grant to us an efficient approach.

Thus we have come to an end of this article on ‘Char Array in Java’. If you wish to learn more, check out the Java Course by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring. If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework