为什么我们首先需要 isPrimitive() ?

Why we need isPrimitive() in the first place?

对于这个不清楚的问题,我深表歉意,但我真的不知道为什么我们首先需要 isPrimitive(),因为我无法使用它(抱歉,我只是在需要它时无法使用它;(这里是悲伤的表情)。

阅读各处的帖子后,我发现了一些用法

int.class.isPrimitive()

但我想要一些像

boolean isTrue = true;
System.out.println(isTrue.class.isPrimitive());
System.out.println(Boolean.valueOf(isTrue).getClass().isPrimitive());

我正在尝试在遍历对象的字段时检查类型;我现在能做的就是

private static boolean isPrimitiveWrapper(Object obj) {
    return obj.getClass() == Boolean.class ||
            obj.getClass() == Byte.class ||
            obj.getClass() == Character.class ||
            obj.getClass() == Short.class ||
            obj.getClass() == Integer.class ||
            obj.getClass() == Long.class ||
            obj.getClass() == Float.class ||
            obj.getClass() == Double.class;
}

但是看了一圈,我觉得应该是哪里出了问题,但又不知道是什么问题。

我们将不胜感激 ;)

我正在努力不要太偏执...已经很努力了

由于原始类型在某些情况下不能作为对象处理,例如数组, 作为第一个鉴别器很好。

Object cloneObject(Object obj) {
    Class<?> type = obj == null ? Object.class : obj.getClass();
    if (type.isArray()) {
        Class<?> elemType = type.getComponentType();
        if (!elemType.isPrimitive()) {
            Object[] copy = ...
        } else {
            // Must distinguish between int/double/boolean/...
            ... int[] ... double[] ...
        }
    }

Object inta = new int[] { 2, 3, 5, 7 };
int[] pr = (int[]) cloneObject(inta);