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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Java/J2EE and SOA (16 Blogs)
  • Spring Framework (7 Blogs)
  • PHP and MySQL (11 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

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

61 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

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 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.

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.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.