如何判断object是基本数据类型还是引用数据类型
直接用apache commons下的工具类isPrimitiveOrWrapper即可,没必要造轮子
/*** Returns whether the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},* {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).** @param type* The class to query or null.* @return true if the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},* {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).* @since 3.1*/public static boolean isPrimitiveOrWrapper(final Class<?> type) {if (type == null) {return false;}return type.isPrimitive() || isPrimitiveWrapper(type);}
下面是使用案例
import org.apache.commons.lang3.ClassUtils;Object value = bizParams.get(key);if (ClassUtils.isPrimitiveOrWrapper(value.getClass()) || value instanceof String) {//基本数据类型或者是String} else {// 引用数据类型}