0

How can we identify the primitive data type of a given variable?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Mohit
  • 539
  • 3
  • 9
  • 16

5 Answers5

2

What you are trying to do is of course only applicable if you are working with reflection (and have an Object again). Then you can identify the type with:

field.getType()

or

field.getType().getName()

depending on whether you want the name, or the Class

Then you can compare to Integer.TYPE, Double.TYPE, etc., which are the primitve Class representations.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 2
    I think he means primitives like `int`, etc – skaffman Jan 08 '10 at 14:14
  • Then perhaps he should change the question to reflect that. – Timothy Jan 08 '10 at 14:15
  • you cannot call `getClass()` on a variable that is of a primitive type. the question has a wrong premises - if you are able to compile your program and can reference the variable in question, then you already know the type of that variable. – Chii Jan 08 '10 at 14:15
  • well, you can if it is boxed, but then it returns the wrapper type. I updated my question so that it speaks only of the reflection variant – Bozho Jan 08 '10 at 14:29
1

I assume you mean in Java. The answer depends on who is doing the identification:

If you are a PROGRAMMER and you are reading code, then you can find out the type of the variable by reading upward through the current method looking for a declaration of the variable. If it's not found there, look through the current class for a declaration of an instance variable of that name. Declarations always give the type in Java (unlike Haskell or Scala which are strongly typed but have good type inference) so you will never need to look any further than the variable declaration.

If you are a COMPILER and you are generating code from source, then you can follow the same approach as the programmer. Plus you also have a few extra choices -- in many cases you may be able to determine that the variable doesn't "escape" outside the block of code you are compiling and thus you may never even create the variable... just keep the data in a register.

If you are an EXECUTING PROGRAM, then there is some question of definitions. It's kind of meaningless to find the type of a variable -- variables are just labels in the code, what really exists at runtime is the objects stored in these variables. It is however, quite plausible that for some object type you might have a variable of some generic type and want to know what the actual type is of the real instance. (For primitive types there is no subclassing, so the issue could never come up.) For instance, you might have this:

public void someFunc(Animal animal) {
    // Here I want to know if 'animal' is a 'Dog' or a 'Cat'
}

In that case, you can use the getClass() method which is present on all Objects in Java:

public void someFunc(Animal animal) {
    System.out.println("The type of animal is: " + animal.getClass());
}

Hope this helps!

mcherm
  • 23,999
  • 10
  • 44
  • 50
  • 1
    If the OP is a compiler or an executing program, we have already lost the Great Robot War :( Humanity was nice while it lasted, I guess. – Alex Oct 10 '11 at 15:26
0

I don't think that you can.

I mean... if I create an object of type "Student", what would be the primitive? Doesn't make sense.

Timothy
  • 2,457
  • 19
  • 15
0

You can try :

char a = '0'; //any primitive
Object obj = a;
System.out.println(obj.getClass().getName());

In this case it will print : java.lang.Character

Divya Jha
  • 21
  • 5
-4

I think you are looking for something along this lines

private final static Map<Class<?>, Class<?>> simpleTypes = new Hashtable<Class<?>, Class<?>>();
static {
    simpleTypes.put(String.class, String.class);
    simpleTypes.put(Boolean.class, Boolean.class);
    simpleTypes.put(boolean.class, boolean.class);
    simpleTypes.put(Byte.class, Byte.class);
    simpleTypes.put(byte.class, byte.class);
    simpleTypes.put(Short.class, Short.class);
    simpleTypes.put(short.class, short.class);
    simpleTypes.put(Integer.class, Integer.class);
    simpleTypes.put(int.class, int.class);
    simpleTypes.put(Long.class, Long.class);
    simpleTypes.put(long.class, long.class);
    simpleTypes.put(Float.class, Float.class);
    simpleTypes.put(float.class, float.class);
    simpleTypes.put(Double.class, Double.class);
    simpleTypes.put(double.class, double.class);
    simpleTypes.put(Character.class, Character.class);
    simpleTypes.put(char.class, char.class);
    simpleTypes.put(BigDecimal.class, BigDecimal.class);
    simpleTypes.put(StringBuffer.class, StringBuffer.class);
    simpleTypes.put(BigInteger.class, BigInteger.class);
    simpleTypes.put(Class.class, Class.class);
    simpleTypes.put(java.sql.Date.class, java.sql.Date.class);
    simpleTypes.put(java.util.Date.class, java.util.Date.class);
    simpleTypes.put(Time.class, Time.class);
    simpleTypes.put(Timestamp.class, Timestamp.class);
    simpleTypes.put(Calendar.class, Calendar.class);
    simpleTypes.put(GregorianCalendar.class, GregorianCalendar.class);
    simpleTypes.put(URL.class, URL.class);
    simpleTypes.put(Object.class, Object.class);
}


public static boolean isSimpleType(final Object object) {
    if (object == null) { return false; }
    return isSimpleType(object.getClass());
}

public static boolean isSimpleType(final Class<?> clazz) {
    if (clazz == null) { return false; }
    return simpleTypes.containsKey(clazz);
}



public static boolean isMapType(final Class<?> clazz) {
    boolean lvReturn = false;
    if (clazz != null) {
        lvReturn = (Map.class.isAssignableFrom(clazz));
    }
    return lvReturn;
}

public static boolean isMapType(final Object object) {
    boolean lvReturn = false;
    if (object == null) { 
        lvReturn = false;
    }
    else if (object instanceof Map) {
        lvReturn = true;
    }

    return lvReturn;
}

public static boolean isCollection(final Object object) {
    boolean lvReturn = false;
    if (object == null) { 
        lvReturn = false;
    }else{
        lvReturn=isCollection(object.getClass());
    }
    return lvReturn;
}

public static boolean isCollection(final Class<?> clazz) {
    boolean lvReturn = false;
    if (clazz == null) {
        lvReturn = false;
    }
    else if (Collection.class.isAssignableFrom(clazz)) {
        lvReturn = true;
    } 
    return lvReturn;
}

public static boolean isArray(final Object obj) {
    if (obj == null) {
        return false;
    }       
    return isArray(obj.getClass());
}

public static boolean isArray(final Class<?> clazz) {
    boolean lvReturn = false;
    if (clazz == null) {
        lvReturn = false;
    }else if(clazz.isArray()) {
        lvReturn = true;
    }
    return lvReturn;
}


public static boolean isEnum(final Object obj) {
    if (obj == null) {
        return false;
    }       
    return isEnum(obj.getClass());
}

public static boolean isEnum(final Class<?> clazz) {
    boolean lvReturn = false;
    if (clazz == null) {
        lvReturn = false;
    }else if (clazz.isEnum()) {
        lvReturn = true;
    }
    return lvReturn;
}
Greg
  • 1,671
  • 2
  • 15
  • 30
  • 3
    WTF? Are you paid for each line of code you're writing? Just out of curiosity: For example in the isMapType(Object) method; why did you need 8 lines of code to express "return object instanceof Map;"? – jarnbjo Jan 08 '10 at 15:10
  • FYI:Not my code , but what is the problem with checking if the object is null or not, but I do agree with boolean lvReturn = false; that is useless in my opion. – Greg Jan 08 '10 at 16:08
  • This is one of the worst things I've ever seen. Is there a downmod drought or something? Why do you have a giant map where every key maps to itself? – danben Jan 08 '10 at 16:45
  • This code is taken from a Object Serializer similar to XStream so not really sure, I assume that they use a map because of O(1) performance rather than a list of O(n), someone give me my point back :) – Greg Jan 08 '10 at 17:09
  • If you want to check that an element exists within a set you should use... a Set! :) – Philippe Beaudoin Mar 17 '11 at 17:51