Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Using Reflection API to determine whether class object is an Array or not

Program to demonstrate how Reflection API can be used to determine whether a class object is an Array or not in Java

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 :



 
© 2021 Learn Java by Examples Template by Hubberspot