In computer programs, arrays are useful to group and access multiple elements of the same type. In Java, arrays (both single- and multi-dimensional) can be created from either primitive types (such as int, float, etc.) or classes. Every array belongs to a class that contains information on the type of array elements and the number of dimensions; as with any other class, the Class object corresponding to an array can be retrieved with the getClass() method of the java.lang.Object class.

Given a Class object, we can check whether it corresponds to an array object with the isArray() method, which returns true for array objects and false otherwise. The following example demonstrates the use of this method. After creating an array of integer values and a String object, we retrieve the corresponding Class objects and then we call isArray() on these objects.


public class IdentifyArray {
  public static void main(String str[]){
    int[] arrayObj = {1,2,3,4};
    String stringObj = "Hello";
    Class arrayClass = arrayObj.getClass();
    Class stringClass = stringObj.getClass();
    if (arrayClass.isArray())
      System.out.println("arrayClass is an array");
      System.out.println("arrayClass is not an array");
    if (stringClass.isArray())
      System.out.println("stringClass is an array");
      System.out.println("stringClass is not an array");

As illustrated by the program output reported below, the isArray() method returns true for arrayClass and false for stringClass.

arrayClass is an array
stringClass is not an array