Monday, March 5, 2012

How do I check if a class represent a primitive type in Java?


Java uses class objects to represent all eight primitive types. A class object that represents a primitive type can be identified using the isPrimitive() method call.void is not a type in Java, but theisPrimitive() method returns true for void.class.

public class IsPrimitiveDemo {
    public static void main(String[] args) {
        IsPrimitiveDemo.get(int.class);
        IsPrimitiveDemo.get(String.class);
        IsPrimitiveDemo.get(double.class);
        IsPrimitiveDemo.get(void.class);
    }

    private static void get(Class clazz) {
        if (clazz.isPrimitive()) {
            System.out.println(clazz.getName() +
                    " is a primitive type.");
        } else {
            System.out.println(clazz.getName() +
                    " is not a primitive type.");            
        }
    }
}

Below is the output...

int is a primitive type.
java.lang.String is not a primitive type.
double is a primitive type.
void is a primitive type.



No comments:

Post a Comment