Home / DeveloperSection / Articles / Arrays in Java: Determining Class of an Array (Part-8)

Arrays in Java: Determining Class of an Array (Part-8)

David Miller 1647 13-May-2016

In previous posts, we have learnt how to find the length of the arrays and how to clone an array. Now here we learn how to find out the class of an array.

public class MindStickArrayClass
        public static void main(String[] args)
              {                      final int SIZE = 5;
                   int[] integerArray = new int[SIZE];
                   float[] floatArray = { 5.0f, 3.0f, 2.0f, 1.5f };
                   String[] weekDays = { 'Sunday', 'Monday', 'Tuesday', 'Wednesday',
                                                  'Thursday', 'Friday', 'Saturday' };
                   int[][] jaggedArray = { { 5, 4 }, { 10, 15, 12, 15, 18 }, { 6, 9, 10 },
                                                 { 12, 5, 8, 11 } };
                   Class cls = integerArray.getClass();
                   System.out.println('The class name of integerArray: ' + cls.getName());                    cls = floatArray.getClass();
                   System.out.println('The class name of floatArray: ' + cls.getName());
                 cls = weekDays.getClass();
                   System.out.println('The class name of weekDays: ' + cls.getName());                  cls = jaggedArray.getClass();
                   System.out.println('The class name of jaggedArray: ' + cls.getName());
                   cls = cls.getSuperclass();
                   System.out.println('The super class of an array object: '
                                                + cls.getName());          }



The main method declares four arrays of different data types, as in our earlier example from length determination. To obtain the class represented by an array object, we use the expression arrayName.getClass(). The getClass method is a method of the Object class that returns a class representation of the given object. Java defines a class called Class that provides (presents) this class representation. We obtain this class object in the following statement:

Class cls = integerArray.getClass();

Now, to print the name of the obtained class, we call the getName method of the Class class on the object cls. The following statement prints this name to the console:

System.out.println('The class name of integerArray: ' + cls.getName());

Now, study the following program output: 

The class name of integerArray: [I

he class name of floatArray: [F
he class name of weekDays: [Ljava.lang.String;
The class name of jaggedArray: [[I

 The super class of an array object: java.lang.Object

·         The class name of the single-dimensional integer array is [I.

·         The class name of the two-dimensional integer array is [[I.

·         The name of the floating-point array class is [F,

·         and finally the name of the local String type array class is [Ljava.lang.String.

 Although it is not explicitly stated anywhere in the Java language specifications, we can observe that the dimensions of the represented array object are indicated by the number of open square brackets and that the type of element is indicated by a trailing single character in the case of primitive types. In the case of a local class, the fully qualified name of the class is appended after the square bracket. For an array of the Object class, the name is [Ljava.lang.Object, and for an array of the ArrayClassNameApp class, the name is [LArrayClassNameApp.

Updated 16-Dec-2017
David Miller

David Miller

Leave Comment

Please enter subject.
Please enter comments message.


Liked By