Pin It

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 :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot