简体   繁体   English

你怎么知道java中的变量类型?

[英]How do you know a variable type in java?

Let's say I declare a variable: 假设我声明了一个变量:

String a = "test";

And I want to know what type it is, ie, the output should be java.lang.String How do I do this? 我想知道它是什么类型,即输出应该是java.lang.String我该怎么做?

a.getClass().getName()

If you want the name, use Martin's method. 如果您想要名称,请使用Martin的方法。 If you want to know whether it's an instance of a certain class: 如果你想知道它是否是某个类的实例:

boolean b = a instanceof String

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

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. 无论如何,它可能会有所帮助。

His solution was: 他的解决方案是:

a.getClass().getName()

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. 在这种情况下,原语将简单地包装在Wrapper中。 You will get the Object of the primitive in that case. 在这种情况下,您将获得基元的Object。

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

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 ...

}

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实现......但你最好找到另一个解决方案,我真的好奇为什么你需要变量类型,而不仅仅是值类型?

I think we have multiple solutions here: 我想我们有多个解决方案:

  • instance of could be a solution. 实例可能是一个解决方案。

Why? 为什么? In Java every class is inherited from the Object class itself. 在Java中,每个类都继承自Object类本身。 So if you have a variable and you would like to know its type. 所以如果你有一个变量,你想知道它的类型。 You can use 您可以使用

  • System.out.println(((Object)f).getClass().getName()); 的System.out.println(((对象)F).getClass()的getName());

or 要么

  • Integer.class.isInstance(1985); Integer.class.isInstance(1985); // gives true //给出了真实

or 要么

  • isPrimitive() 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: 这会给我们:

  1. FALSE
  2. 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

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

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