简体   繁体   中英

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

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?

Ie

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

The need for such information can be eg 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.

I found something:

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

ClassUtils.isPrimitiveOrWrapper()

Spring :

BeanUtils.isSimpleValueType()

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

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

Class.isPrimitive :

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

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

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;
}

The java.util.Class type has the proper methods:

Class<?> type = ...

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

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

value instanceof 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.

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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM