Java. Check if object is primitive

isPrimitive method

There is isPrimitive() method in java.lang.Class

int intVariable = 10;
Class clazz = int.class;
boolean isIntPrimitive = class.isPrimitive();

Looks a little stupid, right?

You already know that this is primitive because you write int.class.

You’d like to have something like public boolean isPrimitive(Object object).

    private boolean isPrimitive(Object object) {
        Class clazz = object.getClass();
        return clazz.isPrimitive();
    }

But this will not work.

The problem is that when you call isPrimitive(intVariable) intVariable will be auto-boxed to Integer which is already not a primitive. So for int intVariable our method will return false.

Let’s take a look at some “primitive wrappers”

Check if object is Integer

public boolean isInteger(Object object) {
    return object != null && object.getClass() == Integer.class
}

Check if object is Boolean

public boolean isBoolean(Object object) {
    return object != null && object.getClass() == Boolean.class
}

Check if object is Character

public boolean isCharacter(Object object) {
    return object != null && object.getClass() == Character.class
}

Check if object is Byte

public boolean isByte(Object object) {
    return object != null && object.getClass() == Byte.class
}

Check if object is Short

public boolean isShort(Object object) {
    return object != null && object.getClass() == Short.class
}

Check if object is Long

public boolean isLong(Object object) {
    return object != null && object.getClass() == Long.class
}

Check if object is Float

public boolean isFloat(Object object) {
    return object != null && object.getClass() == Float.class
}

Check if object is Double

public boolean isDouble(Object object) {
    return object != null && object.getClass() == Double.class
}

So all we can do is to check if an object is a primitive in wider meaning - “wrapper for primitive”

Check if object is primitive wrapper

public boolean isWiderPrimitive(Object object) {
    if (object == null) {
        return false;
    }
    Class clazz = object.getClass();
    if (clazz == Boolean.class || clazz == Character.class || 
        clazz == Byte.class || clazz == Short.class ||
        clazz == Integer.class || clazz == Long.class || 
        clazz == Float.class || clazz == Double.class) {
        return true;
    }
    return false;
}

Since this is “isWiderPrimitive” I would include also String and Void (yes there is the Void class in Java)

The final version of our isWiderPrimitive method looks like this

public boolean isWiderPrimitive(Object object) {
    if (object == null) {
        return false;
    }
    Class clazz = object.getClass();
    if (clazz == Boolean.class || clazz == Character.class || 
        clazz == Byte.class || clazz == Short.class ||
        clazz == Integer.class || clazz == Long.class || 
        clazz == Float.class || clazz == Double.class ||
        clazz == String.class || clazz == Void.class) {
        return true;
    }
    return false;
}