Java:如何确定类型是原始类型/包装器/字符串还是其他类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25039080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:11:28  来源:igfitidea点击:

Java: How to determine if type is any of primitive/wrapper/String, or something else

javatypes

提问by Ondra ?i?ka

Is there a single method in JDK or common basic libraries which returns true if a type is a primitive, a primitive wrapper, or a String?

如果类型是原始类型、原始包装器或字符串,JDK 或通用基本库中是否有单个方法返回 true?

I.e.

IE

Class<?> type = ...
boolean isSimple = SomeUtil.isSimple( type );

The need for such information can be e.g. to check whether some data can be represented in formats like JSON. The reason for a single method is to be able to use it in expression language or templates.

对此类信息的需求可以是例如检查某些数据是否可以以 JSON 等格式表示。单一方法的原因是能够在表达式语言或模板中使用它。

回答by Ondra ?i?ka

I found something:

我发现了一些东西:

Commons Lang: (would have to combine with check for String)

Commons Lang:(必须结合检查字符串)

ClassUtils.isPrimitiveOrWrapper()

Spring:

春天:

BeanUtils.isSimpleValueType()

This is what I want, but would like to have it in Commons.

这就是我想要的,但希望在 Commons 中拥有它。

回答by Boann

Is there a single method which returns true if a type is a primitive

如果类型是原始类型,是否有一个返回 true 的方法

Class.isPrimitive:

类.isPrimitive:

Class<?> type = ...;
if (type.isPrimitive()) { ... }

Note that void.class.isPrimitive()is true too, which may or may not be what you want.

请注意,这void.class.isPrimitive()也是正确的,这可能是您想要的,也可能不是。

a primitive wrapper?

原始包装器?

No, but there are only eight of them, so you can check for them explicitly:

不,但只有八个,因此您可以明确检查它们:

if (type == Double.class || type == Float.class || type == Long.class ||
    type == Integer.class || type == Short.class || type == Character.class ||
    type == Byte.class || type == Boolean.class) { ... }

a String?

一个字符串?

Simply:

简单地:

if (type == String.class) { ... }

That's not one method. I want to determine whether it's one of those named or something else, in one method.

那不是一种方法。我想用一种方法确定它是那些命名的还是其他的。

Okay. How about:

好的。怎么样:

public static boolean isPrimitiveOrPrimitiveWrapperOrString(Class<?> type) {
    return (type.isPrimitive() && type != void.class) ||
        type == Double.class || type == Float.class || type == Long.class ||
        type == Integer.class || type == Short.class || type == Character.class ||
        type == Byte.class || type == Boolean.class || type == String.class;
}

回答by icza

The java.util.Classtype has the proper methods:

java.util.Class类型具有适当的方法:

Class<?> type = ...

boolean primitive = type.isPrimitive();
boolean string_ = type == String.class;
boolean array = type.isArray();
boolean enum_ = type.isEnum();
boolean interf_ = type.isInterface();

回答by Joachim Sauer

Guava provides the Primitivesclass with Primitives.isWrapperType(class)which returns truefor Integer, Long, ...

番石榴提供了PrimitivesPrimitives.isWrapperType(class)返回trueIntegerLong...

回答by CarlosZ

Integer, Float, Character, etc are not primitives; they are wrapper classes which serve as containers for primitives. They are reference objects. True primitives are types like int, float, double, long, byte, char, and boolean -- non-object types. There's a big difference, since

Integer、Float、Character 等不是基元;它们是包装类,用作原语的容器。它们是参考对象。真正的原语是像 int、float、double、long、byte、char 和 boolean 这样的类型——非对象类型。有很大的不同,因为

value instanceof Float

Float的值实例

won't even compile if "value" is a primitive. "String" is also not a primitive -- it's a type of object. 'null' is also not a primitive -- it's a literal value.

如果“值”是原始值,甚至不会编译。“String”也不是原始类型——它是一种对象。'null' 也不是原始值——它是一个字面值。

回答by Damian Leszczyński - Vash

No there is not. And should not be. For tree difference question you should provide tree different answers.

不,那里没有。而且不应该。对于树差异问题,您应该提供不同的答案。

public static <T> boolean  isPrimitive(Class<T> klass) {

    return klass.isPrimitive();
}

public static <T> boolean isString(Class<T> klass) {

    return String.class == klass; //String is final therefor you can not extend it.

}

public static <T> boolean isPrimitiveWrapper(Class<T> klass) {

    return Character.class == klass || Boolean.class == klass || klass.isAssignableFrom(Number.class);

}