Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

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

Last updated on Feb 29,2024 310.2K Views


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.

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 Online Course Training 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 or you can also join our Java Training in Riyadh.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

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

edureka.co