Check to if a class is a primitive data types in Java

Description

The following code shows how to check to if a class is a primitive data types.

Example


/*from www  .  ja v  a  2 s. 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) {
    isPrimitive(Class.class);
    isPrimitive(Comparable.class);
    isPrimitive(Serializable.class);
    isPrimitive(Integer.class);
    isPrimitive(int.class);
    isPrimitive(Float[].class);
    isPrimitive(String.class);
    isPrimitive(double[].class);
    isPrimitive(boolean.class);

  }
  /** 
   * Demonstrates usage of <tt>isPrimitive</tt> from Class.
   *
   * @param dataType The data type to check.
   *
   * @throws NullPointerException If the user passes null for dataType.
   */
  public static void isPrimitive(final Class dataType) {
    if (dataType == null) {
      throw new NullPointerException();
    }
    if (dataType.isPrimitive()) {
      System.out.println(dataType.getName());
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy