java 我们如何识别给定变量的原始数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2028073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:00:33  来源:igfitidea点击:

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

java

提问by Mohit

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

我们如何识别给定变量的原始数据类型?

回答by Bozho

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

您正在尝试做的当然仅适用于您正在使用反射(并Object再次使用)。然后您可以通过以下方式识别类型:

field.getType()

or

或者

field.getType().getName()

depending on whether you want the name, or the Class

取决于你是想要名字,还是 Class

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

然后,你可以比较Integer.TYPEDouble.TYPE等等,这些都是primitveClass表示。

回答by mcherm

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

我假设你的意思是在 Java 中。答案取决于谁在进行识别:

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.

如果你是一名程序员并且你正在阅读代码,那么你可以通过向上阅读当前方法寻找变量的声明来找出变量的类型。如果在那里找不到,请查看当前类以查找该名称的实例变量的声明。声明总是在 Java 中给出类型(与 Haskell 或 Scala 不同,它们是强类型但具有良好的类型推断),因此除了变量声明之外,您永远不需要进一步查看。

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 ishowever, 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:

在这种情况下,您可以使用 Java 中所有对象上存在的 getClass() 方法:

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

Hope this helps!

希望这可以帮助!

回答by Timothy

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.

我的意思是……如果我创建一个“Student”类型的对象,那么原始对象是什么?没有意义。

回答by Divya Jha

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

在这种情况下,它将打印: java.lang.Character

回答by Greg

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