你如何知道java中的变量类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2674554/
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
How do you know a variable type in java?
提问by Miguel Ribeiro
Let's say I declare a variable:
假设我声明了一个变量:
String a = "test";
And I want to know what type it is, i.e., the output should be java.lang.String
How do I do this?
我想知道它是什么类型,即输出应该是java.lang.String
How do I do this?
采纳答案by Martin
a.getClass().getName()
回答by Martin Konecny
If you want the name, use Martin's method. If you want to know whether it's an instance of a certain class:
如果您需要名称,请使用 Martin 的方法。如果你想知道它是否是某个类的实例:
boolean b = a instanceof String
boolean b = a instanceof String
回答by Sebastien Lorber
I agree with what Joachim Sauer said, not possible to know (the variable type! not value type!) unless your variable is a class attribute (and you would have to retrieve class fields, get the right field by name...)
我同意 Joachim Sauer 所说的,不可能知道(变量类型!不是值类型!)除非您的变量是类属性(并且您必须检索类字段,按名称获取正确的字段......)
Actually for me it's totally impossible that any a.xxx().yyy()
method give you the right answer since the answer would be different on the exact same object, according to the context in which you call this method...
实际上对我来说,任何a.xxx().yyy()
方法都完全不可能为您提供正确的答案,因为根据您调用此方法的上下文,答案在完全相同的对象上会有所不同......
As teehoo said, if you know at compile a defined list of types to test you can use instanceof but you will also get subclasses returning true...
正如 teehoo 所说,如果你知道在编译一个定义的类型列表来测试你可以使用 instanceof 但你也会得到返回 true 的子类......
One possible solution would also be to inspire yourself from the implementation of java.lang.reflect.Field
and create your own Field
class, and then declare all your local variables as this custom Field
implementation... but you'd better find another solution, i really wonder why you need the variable type, and not just the value type?
一种可能的解决方案也是从实现java.lang.reflect.Field
和创建自己的Field
类中激发自己的灵感,然后将所有局部变量声明为这个自定义Field
实现......但你最好找到另一种解决方案,我真的想知道你为什么需要这个变量类型,而不仅仅是值类型?
回答by Copy_Paste
I learned from the Search Engine(My English is very bad , So code...) How to get variable's type? Up's :
我从搜索引擎学到的(我的英文很烂,所以代码...) 如何获取变量的类型?UPS :
String str = "test";
String type = str.getClass().getName();
value: type = java.lang.String
this method :
这种方法:
str.getClass().getSimpleName();
value:String
now example:
现在的例子:
Object o = 1;
o.getClass().getSimpleName();
value:Integer
回答by Atspulgs
I would like to expand on Martin's answer there...
我想在那里扩展马丁的回答......
His solution is rather nice, but it can be tweaked so any "variable type" can be printed like that.(It's actually Value Type, more on the topic). That said, "tweaked" may be a strong word for this. Regardless, it may be helpful.
他的解决方案相当不错,但可以进行调整,以便可以像这样打印任何“变量类型”。(实际上是值类型,更多关于该主题)。也就是说,“调整”可能是一个强烈的词。无论如何,它可能会有所帮助。
Martins Solution:
马丁斯解决方案:
a.getClass().getName()
However, If you want it to work with anything you can do this:
但是,如果您希望它与任何东西一起使用,您可以这样做:
((Object) myVar).getClass().getName()
//OR
((Object) myInt).getClass().getSimpleName()
In this case, the primitive will simply be wrapped in a Wrapper. You will get the Object of the primitive in that case.
在这种情况下,原语将简单地包装在 Wrapper 中。在这种情况下,您将获得原始对象的对象。
I myself used it like this:
我自己是这样使用的:
private static String nameOf(Object o) {
return o.getClass().getSimpleName();
}
Using Generics:
使用泛型:
public static <T> String nameOf(T o) {
return o.getClass().getSimpleName();
}
回答by epicwhat001
Use operator overloading feature of java
使用java的运算符重载特性
class Test {
void printType(String x) {
System.out.print("String");
}
void printType(int x) {
System.out.print("Int");
}
// same goes on with boolean,double,float,object ...
}
回答by Karoly
I think we have multiple solutions here:
我认为我们在这里有多种解决方案:
- instance ofcould be a solution.
- 实例可能是一个解决方案。
Why? In Java every class is inherited from the Object class itself. So if you have a variable and you would like to know its type. You can use
为什么?在 Java 中,每个类都继承自 Object 类本身。所以如果你有一个变量并且你想知道它的类型。您可以使用
- System.out.println(((Object)f).getClass().getName());
- System.out.println(((Object)f).getClass().getName());
or
或者
- Integer.class.isInstance(1985); // gives true
- Integer.class.isInstance(1985); // 给出真
or
或者
isPrimitive()
public static void main(String[] args) { ClassDemo classOne = new ClassDemo(); Class classOneClass = classOne(); int i = 5; Class iClass = int.class; // checking for primitive type boolean retval1 = classOneClass.isPrimitive(); System.out.println("classOneClass is primitive type? = " + retval1); // checking for primitive type? boolean retval2 = iClass.isPrimitive(); System.out.println("iClass is primitive type? = " + retval2); }
isPrimitive()
public static void main(String[] args) { ClassDemo classOne = new ClassDemo(); Class classOneClass = classOne(); int i = 5; Class iClass = int.class; // checking for primitive type boolean retval1 = classOneClass.isPrimitive(); System.out.println("classOneClass is primitive type? = " + retval1); // checking for primitive type? boolean retval2 = iClass.isPrimitive(); System.out.println("iClass is primitive type? = " + retval2); }
This going to give us:
这将给我们:
- FALSE
- TRUE
- 错误的
- 真的
Find out more here: How to determine the primitive type of a primitive variable?
在此处了解更多信息: 如何确定原始变量的原始类型?
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
http://docs.oracle.com/cd/E26806_01/wlp.1034/e14255/com/bea/p13n/expression/operator/Instanceof.html
http://docs.oracle.com/cd/E26806_01/wlp.1034/e14255/com/bea/p13n/expression/operator/Instanceof.html