Is there any way to take a Class and determine if it represents a primitive type (is there a solution that doesn't require specifically enumerating all the primitive types)?
NOTE: I've seen this question. I'm asking basically the opposite. I have the Class, I want to know if it's a primitive.
This method will also check whether it's a wrapper of a primitive type as well:
/**
* Checks first whether it is primitive and then whether it's wrapper is a primitive wrapper. Returns true
* if either is true
*
* @param c
* @return whether it's a primitive type itself or it's a wrapper for a primitive type
*/
public static boolean isPrimitive(Class c) {
if (c.isPrimitive()) {
return true;
} else if (c == Byte.class
|| c == Short.class
|| c == Integer.class
|| c == Long.class
|| c == Float.class
|| c == Double.class
|| c == Boolean.class
|| c == Character.class) {
return true;
} else {
return false;
}
There is a method on the Class object called isPrimitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With