[英]How to convert Integer.class (and other boxed) to int.class (and other primitives)?
有沒有辦法將Integer.class
轉換為int.class
等或 Z65E88000 類型B5C6800AADFC8ZF 的類型88800AADFC896 原語?
就像是:
jshell> Boolean.class.isPrimitive()
$1 ==> false
jshell> Boolean.class.asPrimitive() == boolean.class
$2 ==> true
使用commons-lang的ClassUtils
可以做到這一點:
jshell> import org.apache.commons.lang3.ClassUtils
jshell> ClassUtils.primitiveToWrapper(int.class)
$1 ==> class java.lang.Integer
jshell> ClassUtils.wrapperToPrimitive(Float.class)
$2 ==> float
沒有 Java SE 方法,但很容易保持 Map:
private static final Map<Class<?>, Class<?>> wrapperToPrimitive = Map.of(
Void.class, void.class,
Boolean.class, boolean.class,
Byte.class, byte.class,
Character.class, char.class,
Short.class, short.class,
Integer.class, int.class,
Long.class, long.class,
Float.class, float.class,
Double.class, double.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.