22

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?

I.e.

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.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
  • Prirmitives are not objects, so they are not instances of any classes. All is simple here. To check on wrapper: a instanceof Number. To check on String: str instanceof String. – DmitryKanunnikoff Jul 30 '14 at 14:15
  • possible duplicate of [Determining if an Object is of primitive type](http://stackoverflow.com/questions/709961/determining-if-an-object-is-of-primitive-type) – dimo414 Jul 30 '14 at 14:19
  • See also http://stackoverflow.com/a/11978177/113632 – dimo414 Jul 30 '14 at 14:21
  • 1
    @DmitryTsechoev: 1) You misread the question. There are also `Class` objects for primitive types (see [Double.TYPE](http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#TYPE)) and those should be tested, not the instances of the class / the primitive variable / field. 2) That a object is a instance of `Number` does not imply it's a primitive wrapper (e.g. `BigInteger` is not a primitive wrapper, but it's a `Number`) – fabian Jul 30 '14 at 14:39

6 Answers6

33

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.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
24

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;
}
Boann
  • 48,794
  • 16
  • 117
  • 146
  • That's not one method. I want to determine whether it's one of those named or something else, in one method. – Ondra Žižka Jul 30 '14 at 14:39
  • You say "there are only eight of them". What about `BigInteger`, `BigDecimal` and friends? Can I easily recognise them as also being primitive-like? – Steve Pitchers Apr 26 '17 at 17:39
  • 4
    @StevePitchers The Java Language Specification [defines the eight primitive types explicitly](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2). `BigInteger` and friends are reference types (and quite complicated ones!), although they are intended to operate like primitives by being immutable. Actually that immutability is broken, since they forgot to make those classes `final`. A better candidate for a "primitive-like" reference type would be `String`, or a ["value-based"](https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html) class like `Optional`. – Boann Apr 26 '17 at 18:01
3

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();
icza
  • 389,944
  • 63
  • 907
  • 827
  • 1) That's not a single method, 2) What if it's a subclass of String (i.e. in some Java-using script language) – Ondra Žižka Jul 30 '14 at 14:38
  • 1
    1) I just offered other similar methods which may be of interest to the questioner. 2) `java.lang.String` is a `final` class, so `type` cannot be a subclass of `String`. You can't extend `java.lang.String`. – icza Jul 30 '14 at 14:40
  • Class being final means nothing nowadays. See Javassist. – Ondra Žižka Jul 30 '14 at 14:49
  • If you want to manipulate byte code or the memory, then there is no good solution to anything because it can be modified in the background or the displayed value can be altered. – icza Jul 30 '14 at 14:52
0

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

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
-3

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.

CarlosZ
  • 73
  • 1
  • 8
  • 1
    You should have informed yourself before posting this answer, e.g. in the "TYPE Field for Primitive Type Wrappers" section on http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html . There are `Class` objects for primitive types!!!!! – fabian Jul 30 '14 at 14:24
-5

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

}
  • `isPrimitiveWrapper(BigInteger.class)` will return true, you need to check all wrapper types explicitly. – Celos Sep 19 '16 at 08:05