开发者

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

How can we identify the primitive data 开发者_Go百科type of a given variable?


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.


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!


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.


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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜