Program to demonstrate how Reflection API can be used to determine whether a class object is an Array or not in Java
Output of the program :
package com.hubberspot.reflection;
public class ArrayInfo {
public static void main(String[] args) {
// Create an Array Object
Integer[] intArray = new Integer[] {1,2,3,4} ;
// Create a non-Array Object
Integer integer = new Integer(1);
Class intArrayClass = intArray.getClass();
Class integerClass = integer.getClass();
// isArray() checks whether object is of type array or not
if(intArrayClass.isArray()) {
// prints the simple name of the class
System.out.println("Given class " + intArrayClass.getSimpleName()
+ " is an Array");
} else {
System.out.println("Given class " + intArrayClass.getSimpleName()
+ " is not an Array");
}
if(integerClass.isArray()) {
System.out.println("Given class " + integerClass.getSimpleName()
+ " is an Array");
} else {
System.out.println("Given class " + integerClass.getSimpleName()
+ " is not an Array");
}
}
}
Output of the program :
