简体   繁体   English

Java-获取原始数据类型的变量类型

[英]Java - getting variable type for primitive data types

Is it possible to get the data type of an input variable (could be any primitive type, int, bool, float, double) returned as a string by using just one line of code? 是否可以仅使用一行代码来获取以字符串形式返回的输入变量的数据类型(可以是任何原始类型,int,bool,float,double)? I know this can easily be done for a String type using getName and getShortName but I am unsure of how to use these methods to return the type of a primitive type. 我知道使用getNamegetShortName对于String类型可以很容易地做到这一点,但是我不确定如何使用这些方法返回原始类型的类型。 I also want to keep my code very short for doing this preferably using just one line. 我也想使我的代码非常简短,最好只使用一行。

I have searched around and cannot find anywhere this question has been answered in the way I require. 我已经四处搜寻,但找不到以我要求的方式回答这个问题的地方。

What about Class.getName() ? Class.getName()呢?

Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String. 以String形式返回此Class对象表示的实体(类,接口,数组类,原始类型或void)的名称。 If this class object represents a reference type that is not an array type then the binary name of the class is returned, as specified by the Java Language Specification, Second Edition. 如果此类对象表示的引用类型不是数组类型,则返回该类的二进制名称,如Java语言规范第二版所指定。

If this class object represents a primitive type or void, then the name returned is a String equal to the Java language keyword corresponding to the primitive type or void . 如果此类对象表示原始类型或void,则返回的名称是一个String,该字符串等于对应于原始类型或void的Java语言关键字

You can follow this link in the Java Doc that explain very well how to use this method. 您可以在Java文档中单击此链接该链接很好地解释了如何使用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM