Java char Array

It is used to store char data type values only. With the following examples you can learn how to declare char array, how to assign values to char array and how to get values from char array.

char Array Example
/* char Array Example */
/* Save with file name CharArray.java */

public class CharArray
{
	public static void main(String args[])
	{
		//CHAR ARRAY DECLARATION
		char c[];
		//MEMORY ALLOCATION FOR CHAR ARRAY
		c = new char[4];
		//ASSIGNING ELEMENTS TO CHAR ARRAY
		c[0] = 'a';
		c[1] = 'c';
		c[2] = 'D';
		c[3] = 'B';
		//CHAR ARRAY OUTPUT
		System.out.println();
		System.out.println("char Array Example");
		System.out.println("==================");
		System.out.println();
		for(int i=0;i<c.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + c[i]);
		}
	}
}
                        

In this example you can learn how to assign values to char array at the time of declaration.

char Array Example 2
/* char Array Example 2 */
/* Save with file name CharArray2.java */

public class CharArray2
{
	public static void main(String args[])
	{
		//CHAR ARRAY DECLARATION AND ASSIGNMENT
		char c[] = {'a', 'c', 'D', 'B'};
		//CHAR ARRAY OUTPUT
		System.out.println();
		System.out.println("char Array Example");
		System.out.println("==================");
		System.out.println();
		for(int i=0;i<c.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + c[i]);
		}
	}
}
                        

In this example you can learn how to declare char array with other char variables.

char Array Example 3
/* char Array Example 3 */
/* Save with file name CharArray3.java */

public class CharArray3
{
	public static void main(String args[])
	{
		//CHAR ARRAY DECLARATION
		char c[], a;//c IS AN ARRAY a IS NOT AN ARRAY
		//MEMORY ALLOCATION FOR CHAR ARRAY
		c = new char[4];
		//ASSIGNING ELEMENTS TO CHAR ARRAY
		c[0] = 'a';
		c[1] = 'c';
		c[2] = 'D';
		c[3] = 'B';
		a = 'X';
		//CHAR ARRAY OUTPUT
		System.out.println();
		System.out.println("char Array Example");
		System.out.println("==================");
		System.out.println();
		System.out.println("a value is : "+a);
		System.out.println();
		for(int i=0;i<c.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + c[i]);
		}
	}
}
                        

In this example you can learn how to assign char array to other char array.

char Array Example 4
/* char Array Example 4 */
/* Save with file name CharArray4.java */

public class CharArray4
{
	public static void main(String args[])
	{
		//CHAR ARRAY DECLARATION
		char[] c, a;//c AND a ARE ARRAY VARIABLES
		//MEMORY ALLOCATION FOR CHAR ARRAY
		c = new char[4];
		//ASSIGNING ELEMENTS TO CHAR ARRAY
		c[0] = 'a';
		c[1] = 'c';
		c[2] = 'D';
		c[3] = 'B';
		//ASSIGNING c ARRAY TO a ARRAY VARIABLE
		a = c;
		//CHAR ARRAY OUTPUT
		System.out.println();
		System.out.println("char Array Example");
		System.out.println("==================");
		System.out.println();
		System.out.println("c array values");
		for(int i=0;i<c.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + c[i]);
		}
		System.out.println();
		System.out.println("a array values");
		for(int i=0;i<a.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + a[i]);
		}
	}
}