2

This is what I tried:

private boolean isBoxedPrimitive(Class<?> type) {
    return type == Integer.class ||
            type == Long.class ||
            type == Short.class ||
            type == Byte.class ||
            type == Float.class ||
            type == Double.class ||
            type == Boolean.class ||
            type == Character.class;
}

Is there a better way to write this in java, other than just enumerating 8 classes?

  • May we ask the context in which you would need such a method? Each of the types you are checking above behaves differently, and it would be unwieldy to try to write a single method to handle all types of inputs. – Tim Biegeleisen Oct 01 '19 at 08:43
  • 2
    https://stackoverflow.com/a/3831485/7546121 – Amir Schnell Oct 01 '19 at 08:43
  • 2
    Even if your function returns `true`, it does not mean it is a boxed primitive; it may be an instance of one of the classes that has been created as such rather than having been boxed. – daniu Oct 01 '19 at 08:45
  • 1
    No (besided using a static final Set instead), it would have been nice if all implemented some `BoxedPrimitive`, but no. However simply use `==` – Joop Eggen Oct 01 '19 at 08:53
  • @AmirSchnell ok thanks, I've added my answer there – KraftDurchBlumen Oct 01 '19 at 09:25

0 Answers0