Download Java char Array in Formats
QR Code for this Page
Java char Array
Complete Tutorial on Java primitive char data type array.

Java char Array

char Array in Java

Java char array is used to store char data type values only. In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NUL character). The Java platform uses the UTF-16 representation in char arrays and in the String and StringBuffer classes.

With the following Java char array examples you can learn

  • how to declare Java char array
  • how to assign values to Java char array
  • how to get values from Java char array

What is a char in Java ?

char is a primitive data type in Java. char is a any character of a Java character set. The default value of a char data type is '\u0000'. char variable capable of storing following values. The literal char enclosed with single quotes.

  • char can store any alphabet.
  • char can store a number 0 to 65535.
  • char can store a special character. E.g. !, @, #, $, %, ^, &, *, (, ), ¢, £, ¥
  • char can store unicode (16 bit) character.

How to Declare char Array in Java ?

Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type.

If you put [] (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4.

//declaration of char array
char[] charArray;

How to Initialize char Array in Java ?

The char array will be initialized to '\u0000' when you allocate it. All arrays in Java are initialized to the default value for the type. This means that arrays of ints are initialised to 0, arrays of booleans are initialised to false and arrays of reference types are initialised to null.

//initialize char array
char[] charArray = new char[10];

What is the Length of an Array in Java ?

In Java all the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647. If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collections framework or simply use Vector.

Syntax :
1
2
< data type > < variable >[];
< data type >[] < variable >;

Java char Array Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*  Java char Array Example
    Save with file name CharArray.java  */
  
public class CharArray
{
  public static void main(String args[])
  {
    //JAVA CHAR ARRAY DECLARATION
    char c[];
    //MEMORY ALLOCATION FOR JAVA CHAR ARRAY
    c = new char[4];
    //ASSIGNING ELEMENTS TO JAVA CHAR ARRAY
    c[0] = 'a';
    c[1] = 'c';
    c[2] = 'D';
    c[3] = 'B';
    //JAVA CHAR ARRAY OUTPUT
    System.out.println("Java char Array Example");
    for(int i=0;i<c.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + c[i]);
    }
  }
}

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

How to assign values to char array at the time of declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*  How to assign values to char array at the time of declaration Example
    Save with file name CharArray2.java */
  
public class CharArray2
{
  public static void main(String args[])
  {
    //JAVA CHAR ARRAY DECLARATION AND ASSIGNMENT
    char c[] = {'a', 'c', 'D', 'B'};
    //JAVA CHAR ARRAY OUTPUT
    System.out.println("Java char Array Example");
    for(int i=0;i<c.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + c[i]);
    }
  }
}

Following Java char array example you can learn how to declare Java char array with other Java char array variables.

How to declare Java char array with other Java char array variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*  How to declare Java char array with other Java char array variables Example
    Save with file name CharArray3.java */
  
public class CharArray3
{
  public static void main(String args[])
  {
    //JAVA CHAR ARRAY DECLARATION
    //c IS AN ARRAY a IS NOT AN ARRAY
    char c[], a;
    //MEMORY ALLOCATION FOR JAVA CHAR ARRAY
    c = new char[4];
    //ASSIGNING ELEMENTS TO JAVA CHAR ARRAY
    c[0] = 'a';
    c[1] = 'c';
    c[2] = 'D';
    c[3] = 'B';
    a = 'X';
    //JAVA CHAR ARRAY OUTPUT
    System.out.println("Java char Array Example");
    System.out.println("a value is : "+a);
    for(int i=0;i<c.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + c[i]);
    }
  }
}

Following Java char array example you can learn how to assign Java char array to other Java char array.

How to assign Java char array to other Java char array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*  How to assign Java char array to other Java char array Example
    Save with file name CharArray4.java */
  
public class CharArray4
{
  public static void main(String args[])
  {
    //JAVA CHAR ARRAY DECLARATION
    char[] c, a;//c AND a ARE ARRAY VARIABLES
    //MEMORY ALLOCATION FOR JAVA CHAR ARRAY
    c = new char[4];
    //ASSIGNING ELEMENTS TO JAVA CHAR ARRAY
    c[0] = 'a';
    c[1] = 'c';
    c[2] = 'D';
    c[3] = 'B';
    //ASSIGNING c ARRAY TO a ARRAY VARIABLE
    a = c;
    //JAVA CHAR ARRAY OUTPUT
    System.out.println("Java char Array Example");
    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("a array values");
    for(int i=0;i<a.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + a[i]);
    }
  }
}

How to Convert String to char Array in Java ?

Sometimes you need to convert String to the character array in java programs or convert a String to char from specific index. String class has three methods related to char. Let’s look at them before we look at a Java program to convert string to char array. Let’s look at a simple string to char array java program example.

  1. char[] toCharArray() : This method converts string to character array. The char array size is same as the length of the string.
  2. char charAt(int index) : This method returns character at specific index of string. This method throws StringIndexOutOfBoundsException if the index argument value is negative or greater than the length of the string.
  3. getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) : This is a very useful method when you want to convert part of string to character array. First two parameters define the start and end index of the string; the last character to be copied is at index srcEnd-1. The characters are copied into the char array starting at index dstBegin and ending at dstBegin + (srcEnd-srcBegin) – 1.

The following example converting string to char array and string to char Java.

String to char array java – convert string to char

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*  How to Convert String to char Array in Java Example
    Save with file name StringToCharArrayJava
 
public class StringToCharArrayJava
{
  public static void main(String[] args)
  {
    String str = "How to Convert String to char Array in Java";
    //CONVERT STRING TO CHAR ARRAY
    char[] string_char_array = str.toCharArray();
    System.out.println(string_char_array.length);
    //CHAR AT SPECIFIED INDEX
    char c = str.charAt(2);
    System.out.println(c);
    //COPY PART OF STRING TO CHAR ARRAY
    char[] char_array = new char[8];
    str.getChars(0, 8, char_array, 0);
    System.out.println(char_array);
  }
}
Download Java char Array in Formats