How to Test If Type Is Primitive

How To Test if Type is Primitive

You can use the property Type.IsPrimitive, but be carefull because there are some types that we can think that are primitives, but they aren´t, for example Decimal and String.

Edit 1: Added sample code

Here is a sample code:

if (t.IsPrimitive || t == typeof(Decimal) || t == typeof(String) || ... )
{
// Is Primitive, or Decimal, or String
}

Edit 2: As @SLaks comments, there are other types that maybe you want to treat as primitives, too. I think that you´ll have to add this variations one by one.

Edit 3: IsPrimitive = (Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single),
Anther Primitive-Like type to check (t == typeof(DateTime))

Determining if an Object is of primitive type

The types in an Object[] will never really be primitive - because you've got references! Here the type of i is int whereas the type of the object referenced by o is Integer (due to auto-boxing).

It sounds like you need to find out whether the type is a "wrapper for primitive". I don't think there's anything built into the standard libraries for this, but it's easy to code up:

import java.util.*;

public class Test
{
public static void main(String[] args)
{
System.out.println(isWrapperType(String.class));
System.out.println(isWrapperType(Integer.class));
}

private static final Set<Class<?>> WRAPPER_TYPES = getWrapperTypes();

public static boolean isWrapperType(Class<?> clazz)
{
return WRAPPER_TYPES.contains(clazz);
}

private static Set<Class<?>> getWrapperTypes()
{
Set<Class<?>> ret = new HashSet<Class<?>>();
ret.add(Boolean.class);
ret.add(Character.class);
ret.add(Byte.class);
ret.add(Short.class);
ret.add(Integer.class);
ret.add(Long.class);
ret.add(Float.class);
ret.add(Double.class);
ret.add(Void.class);
return ret;
}
}

test if a variable is a primitive rather than an object?

To test for any primitive:

function isPrimitive(test) {
return test !== Object(test);
}

Example:

isPrimitive(100); // true
isPrimitive(new Number(100)); // false

http://jsfiddle.net/kieranpotts/dy791s96/

C# check if type of PropertyInfo is primitive

The main reason is that whenever I do pi.GetType() it says that it's a PropertyInfo.

You should use PropertyType property of PropertyInfo instead of using GetType() method.

Excerpt from documentation:

Gets the type of this property.

So instead of

pi.GetType().IsPrimitive 

use this

pi.PropertyType.IsPrimitive 

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

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.

Check type of primitive field

You're using the wrong constant to check for Long primitives - use Long.TYPE, each other primitive type can be found with a similarly named constant on the wrapper. eg: Byte.TYPE, Character.TYPE, etc.

How do I check if a class represents a particular primitive type

This can be greatly simplified to:

 intClass == int.class

This works because the Class documentation says:

The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.



Related Topics



Leave a reply



Submit