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