Char Array Java

Two arrays are mostly used in programming – String array and Char array. Following program shows the initialization of a character array and also shows how to create an empty array.
Example on Char Array
public class CharArray
  public static void main(String args[])
    char vowels[] = {'a', 'e', 'i', 'o', 'u'};       // initializing char array
    System.out.print("Vowels are: ");
    for(char ch : vowels)
      System.out.print(ch + ", ");

    int x = vowels.length;
    System.out.println("\n\nNumber of vowels: " + x);

    char consonents[] = {  };                      // creating an empty array
    System.out.println("Number of consonents: " + consonents.length);

Char Array
Output screen on Char Array Java

To print the elements new for loop of Java 5 is used.

Know also int array declaration, assignment and initialization.

Leave a Comment

Your email address will not be published.