Check to if a class is an array in Java
Description
The following code shows how to check to if a class is an array.
Example
//from w w w . ja v a 2s. c o m
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Main {
public static void main(final String[] args) {
isArray(Class.class);
isArray(Comparable.class);
isArray(Serializable.class);
isArray(Integer.class);
isArray(int.class);
isArray(Float[].class);
isArray(String.class);
isArray(double[].class);
isArray(boolean.class);
}
/**
* Demonstrates usage of <tt>isArray</tt> from Class.
*
* @param dataType The data type to check.
*
* @throws NullPointerException If the user passes null for dataType.
*/
public static void isArray(final Class dataType) {
if (dataType == null) {
throw new NullPointerException();
}
if (dataType.isArray()) {
System.out.print(dataType.getName());
System.out.println("\t ==> " + dataType.getComponentType());
}
}
}
The code above generates the following result.