java get wrapper class for primitive

Solutions on MaxInterview for java get wrapper class for primitive by the best coders in the world

showing results for - "java get wrapper class for primitive"
Nael
30 Feb 2019
1  // safe because both Long.class and long.class are of type Class<Long>
2  @SuppressWarnings("unchecked")
3  private static <T> Class<T> wrap(Class<T> c) {
4    return c.isPrimitive() ? (Class<T>) PRIMITIVES_TO_WRAPPERS.get(c) : c;
5  }
6
7  private static final Map<Class<?>, Class<?>> PRIMITIVES_TO_WRAPPERS
8    = new ImmutableMap.Builder<Class<?>, Class<?>>()
9      .put(boolean.class, Boolean.class)
10      .put(byte.class, Byte.class)
11      .put(char.class, Character.class)
12      .put(double.class, Double.class)
13      .put(float.class, Float.class)
14      .put(int.class, Integer.class)
15      .put(long.class, Long.class)
16      .put(short.class, Short.class)
17      .put(void.class, Void.class)
18      .build();
19