isArray()

  • How to identify array objects

    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 asint,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 thegetClass() method of thejava.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.