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

java primitive types
java instanceof primitive
int in java
difference between primitive and wrapper class in java
check if object is int java
wrapper class in java oracle
are primitive types objects in java
advantage of wrapper class in java

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?


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.

I found something:

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




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

Determining if an Object is of primitive type, In object-oriented programming, a wrapper class is a class that encapsulates types, so that those types can be used to create object instances and methods in​  In addition to methods to convert to other data types, there's also handy methods to do things like sum two numbers or get the min and max values allowed for the data type. These wrapper classes exist for all primitive data types. Let's have a look at the Javadoc for one of these wrapper classes. This is the Integer class Javadoc.

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


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?


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;

Primitive wrapper class in Java, All other variables in java are object reference types. Primitive types in Here are some examples of how to define primitive data types in Java. A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter. The size of a primitive type depends on the data type, while non-primitive types have all the same size. Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc. You will learn more about these in a later chapter.

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

Java: Understanding Primitive Types and Wrapper Objects, Can you spot the object creation? public static void main(String[] args) { Long sum The fact is it is slower to use the Object wrappers for primitives than just using the If you make different stylistic choices to 99% of the other Java programmers Or you might want to check whether a certain object matches some template;  In this post, we will see how to convert String in Java to different wrapper types and primitive data types supported by Java. 1. Converting String to int (or Integer) We can use the Integer.parseInt() to get the corresponding primitive int value of a String or use Integer.valueOf() to get the corresponding value of Integer wrapper class.

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

When to use primitive vs class in Java?, Every primitive type corresponds to a reference type. we can see that it's required more time to perform the operation for wrapper classes. Java Type Casting. Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting: Widening Casting (automatically) - converting a smaller type to a larger type size byte-> short-> char-> int-> long-> float-> double

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.

Java Primitives versus Objects, Each of eight primitive of java data types has a class dedicated to it, are known because they wrap the primitive data type into an object of that class. Here in we can provide any number as string argument but not the words etc. Similar methods for the other wrapper classes are found in the Java API  In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java.lang.String class. Enclosing your character string within double quotes will automatically create a new String object; for example, String s = "this is a string";.

Java Wrapper Classes, For every primitive type in Java, there is a built-in object type called a wrapper class. The wrapper Click the "Check Me" button to see if you are correct. int​automatic Here are some more useful methods in the Integer and Double classes: Run the code convert an input string to a number so you can do math on it. 13. In order to achieve this, Java provides wrapper classes. All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number. The object of the wrapper class contains or wraps its respective primitive data type. Converting primitive data types into object is called boxing, and this is taken care by the compiler. Therefore, while using a wrapper class you just need to pass the value of the primitive data type to the constructor of the Wrapper class.

2.8. Wrapper Classes, There is a special group of data types (also known as primitive types) that will from one machine architecture to another (as do in most other languages). Also if you are using a wrapper class object then never forget to initialize it to a Best way to Compare String Arrays, Integer Arrays and Strings in Java Check it out. The instanceof operator is used to determine if the array item is an Integer or a String. For strings, you must first narrow the Object to string (see line 8 in the source code) and then use the parseInt method of the Integer class (line 9).

Java Tips: Wherever Possible try to Use Primitive Types Instead of , You can define a class with one or more generic argument. A method that uses this calls MyList with a type argument which is a particular class. Here MyList<​String> , MyList<Integer> . Class functions can be embedded. Wrappers. A primitive type --- boolean, byte, char, double, float, int, long, short  Java has two types of types, as it were. Primitive types and reference types. Primitives are very simple data types which always take up the same amount of memory. An int always takes up 4 bytes.

  • 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.
  • possible duplicate of Determining if an Object is of primitive type
  • See also
  • @DmitryTsechoev: 1) You misread the question. There are also Class objects for primitive types (see Double.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)
  • That's not one method. I want to determine whether it's one of those named or something else, in one method.
  • You say "there are only eight of them". What about BigInteger, BigDecimal and friends? Can I easily recognise them as also being primitive-like?
  • @StevePitchers The Java Language Specification defines the eight primitive types explicitly. 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" class like Optional.
  • 1) That's not a single method, 2) What if it's a subclass of String (i.e. in some Java-using script language)
  • 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.
  • Class being final means nothing nowadays. See Javassist.
  • 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.
  • You should have informed yourself before posting this answer, e.g. in the "TYPE Field for Primitive Type Wrappers" section on . There are Class objects for primitive types!!!!!
  • isPrimitiveWrapper(BigInteger.class) will return true, you need to check all wrapper types explicitly.