Is there an easy way to detect whether a Class such as Integer or Long has an associated primitive type? (e.g. int and long respectively)
And by easy I don’t mean something like maintaining a collection of Classes and checking if that class lives in the collection.
But that is easy. Java has a fixed number of primitive types (7, if I remember them all) and each of those corresponds to exactly one class. It’s very quick and easy to check for membership in an array of 7 elements, or you could put them in a
Setand use that to check for membership in O(1) time. I’m not sure offhand which way would be quicker.If you’re asking whether Java has a built-in method (in the standard API) to do this, I don’t think so. But if there were one, it would probably just check for membership in that set or array of 7 elements.