Java: how to determine if a type is any of the primitive / wrappers / strings, or something else

Is there one method in the JDK or common core libraries that returns true if the type is a primitive, primitive wrapper, or string?

those.

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

The need for such information may be, for example, to check whether some data can be represented in formats such as JSON. The reason for one method is to use it in an expression language or patterns.

+14
source share
6 answers

I found something:

Commons Lang : (would have to be combined with validation for String)

 ClassUtils.isPrimitiveOrWrapper() 

Spring :

 BeanUtils.isSimpleValueType() 

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

+22
source

Is there one method that returns true if the type is primitive

Class.isPrimitive :

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

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

primitive shell?

No, but there are only eight of them, so you can check 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?

Just:

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

This is not one method. I want to determine if this is one of the above or something else, one method.

Good. What 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; } 
+18
source

The java.util.Class type has corresponding methods:

 Class<?> type = ... boolean primitive = type.isPrimitive(); boolean string_ = type == String.class; boolean array = type.isArray(); boolean enum_ = type.isEnum(); boolean interf_ = type.isInterface(); 
+2
source

Guava provides Primitives the Primitives.isWrapperType(class) , which returns true for Integer , Long , ...

0
source

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

instanceof float value

doesn't even compile if the "value" is primitive. "String" is also not primitive - it is an object type. "null" is also not primitive - it is a literal meaning.

-3
source

No no. And it should not be. For a question about tree differences, you must provide a tree of 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); } 
-4
source

Source: https://habr.com/ru/post/973105/


All Articles