Character Array in Java
What is character array in Java ?
The array which contains character elements in it is called character array in Java.The declaration and initialization of character array is done similar to integer array in Java
Syntax to declare character array in Java
data_type arrayName[] = new data_type[arraysize];
Declaration of array is shown in the below given step..
char charr[ ]=new char[3];
Example of character array declaration in Java:
//Program of character array declaration in Java. public class CharacterArray {
public static void main(String[] args) {
char charr[]=new char[5];
charr[0]='c';
charr[1]='h';
charr[2]='e';
charr[3]='l';
charr[4]='p';
for(int i=0;i<charr.length;i++)
{
System.out.print(charr[i]);
}
}
}
Output
In the above example, we have declared a character array of size 5 in which 5 different characters have been assigned to array and diplayed them one by one using for loop.