如何在Java中检查变量的类型?

如何在Java中检查变量的类型?,java,Java,如何检查以确保我的变量是int、数组、double等 编辑:例如,如何检查变量是否为数组?有什么函数可以做到这一点吗?您可以使用Integer而不是int,Double而不是Double,等等(所有基元类型都有这样的类)。 然后您可以使用操作符instanceof,比如if(var instanceof Integer){…}Java是一种静态类型语言,因此编译器会为您执行大部分检查。一旦将变量声明为特定类型,编译器将确保仅为其分配该类型的值(或该类型的子类型的值) 您给出的示例(int、arr

如何检查以确保我的变量是int、数组、double等


编辑:例如,如何检查变量是否为数组?有什么函数可以做到这一点吗?

您可以使用Integer而不是int,Double而不是Double,等等(所有基元类型都有这样的类)。
然后您可以使用操作符instanceof,比如
if(var instanceof Integer){…}

Java是一种静态类型语言,因此编译器会为您执行大部分检查。一旦将变量声明为特定类型,编译器将确保仅为其分配该类型的值(或该类型的子类型的值)

您给出的示例(
int
、array、
double
)都是原语,没有它们的子类型。因此,如果将变量声明为
int

int x;
您可以确定它只会保存
int

但是,如果将变量声明为
列表
,则该变量可能包含
列表
的子类型。这些示例包括
ArrayList
LinkedList

如果您确实有一个
列表
变量,并且需要知道它是否是
数组列表
,则可以执行以下操作:

List y;
...
if (y instanceof ArrayList) { 
  ...its and ArrayList...
}

然而,如果您发现自己认为需要这样做,您可能需要重新思考您的方法。在大多数情况下,如果遵循面向对象的原则,就不需要这样做。当然,每一条规则都有例外。

你问题的第一部分毫无意义。在编译时,没有不知道基元变量类型的情况


关于第二部分,唯一不知道变量是否为数组的情况是它是否为对象。在这种情况下,
object.getClass().isArray()
会告诉您。

实际上,通过滥用Java的方法重载功能,您很容易使用自己的测试仪。尽管我仍然好奇sdk中是否有官方方法

例如:

class Typetester {
    void printType(byte x) {
        System.out.println(x + " is an byte");
    }
    void printType(int x) {
        System.out.println(x + " is an int");
    }
    void printType(float x) {
        System.out.println(x + " is an float");
    }
    void printType(double x) {
        System.out.println(x + " is an double");
    }
    void printType(char x) {
        System.out.println(x + " is an char");
    }
}
StringBuilder randSB = new StringBuilder("just a String");
System.out.println(randSB.getClass().getSimpleName());
然后:

a.getClass().getName()
-将为您提供由
a
引用的实际对象的数据类型,但不是变量
a
最初声明为或随后转换为的数据类型

boolean b=a instanceof String
-将告诉您
a
引用的实际对象是否是特定类的实例。 同样,变量
a
最初声明为或随后转换为的数据类型与instanceof运算符的结果没有关系

我从以下方面获得了这些信息:

这是可能发生的。我试图将
字符串
解析为int,我想知道我的
Integer.parseInt(s.substring(a,b))
在我尝试求和之前是否正在踢出int或垃圾

顺便说一下,这就是所谓的反射。下面是关于这个主题的更多信息:

我使用:
如果(x.getClass()==MyClass.class){…}
公共类演示1 {


}

嗯,我认为可以通过这种方式检查变量的类型

public <T extends Object> void checkType(T object) {    
    if (object instanceof Integer)
        System.out.println("Integer ");
    else if(object instanceof Double)
        System.out.println("Double ");
    else if(object instanceof Float)
        System.out.println("Float : ");
    else if(object instanceof List)
        System.out.println("List! ");
    else if(object instanceof Set)
        System.out.println("Set! ");
}
公共无效检查类型(T对象){
if(整数的对象实例)
System.out.println(“整数”);
else if(Double的对象实例)
系统输出打印项次(“双”);
else if(对象实例of Float)
System.out.println(“Float:”);
else if(列表的对象实例)
System.out.println(“List!”);
else if(集合的对象实例)
System.out.println(“Set!”);
}
这样,您就不需要有多个重载方法。我认为在阵列上使用集合是一种很好的做法,因为它具有额外的好处。话虽如此,我不知道如何检查数组类型。也许有人可以改进这个解决方案。希望这有帮助


是的,我知道这也不能检查原语

我对这些答案都不满意,正确的答案没有解释,也没有反对票,所以我四处搜索,找到了一些资料,并对其进行了编辑,以便于理解。玩一玩吧,不要像人们希望的那样直截了当

//move your variable into an Object type
Object obj=whatYouAreChecking;
System.out.println(obj);

// moving the class type into a Class variable
Class cls=obj.getClass();
System.out.println(cls);

// convert that Class Variable to a neat String
String answer = cls.getSimpleName();
System.out.println(answer);
以下是一种方法:

public static void checkClass (Object obj) {
    Class cls = obj.getClass();
    System.out.println("The type of the object is: " + cls.getSimpleName());       
}
只需使用:

.getClass().getSimpleName();
例如:

class Typetester {
    void printType(byte x) {
        System.out.println(x + " is an byte");
    }
    void printType(int x) {
        System.out.println(x + " is an int");
    }
    void printType(float x) {
        System.out.println(x + " is an float");
    }
    void printType(double x) {
        System.out.println(x + " is an double");
    }
    void printType(char x) {
        System.out.println(x + " is an char");
    }
}
StringBuilder randSB = new StringBuilder("just a String");
System.out.println(randSB.getClass().getSimpleName());
输出:

StringBuilder

如果变量是未初始化的泛型类型,则这些答案都不起作用

根据我所能找到的,只能使用,或通过将初始化参数传递给函数,使其就位,请参见:

<T> T MyMethod(...){ if(T.class == MyClass.class){...}}
这是因为调用方负责在调用之前实例化变量
out
。如果out在调用时为null,这仍然会抛出异常,但与链接解决方案相比,这是迄今为止最简单的方法


我知道这是一种特定的应用程序,但由于这是google上第一个使用java查找变量类型的结果(并且鉴于
T
是一种变量),我觉得应该包括它您可以使用
java.lang.Class.getSimpleName()轻松地检查它
方法,仅当变量具有非基元类型时。它不适用于基本类型int、long等


参考-

基本上,例如:

public class Kerem
{
    public static void main(String[] args)
    {
        short x = 10;
        short y = 3;
        Object o = y;
        System.out.println(o.getClass()); // java.lang.Short
    }

}

我在尝试使用泛型实现类似的功能时遇到了这个问题。通过获取一些答案并添加getClass().isArray(),我得到了以下似乎有效的结果

public class TypeTester <T extends Number>{

<T extends Object> String tester(T ToTest){

    if (ToTest instanceof Integer) return ("Integer");
    else if(ToTest instanceof Double) return ("Double");
    else if(ToTest instanceof Float) return ("Float");
    else if(ToTest instanceof String) return ("String");
    else if(ToTest.getClass().isArray()) return ("Array");
    else return ("Unsure");
}
}
公共类类型测试仪{
串测试器(T测试){
if(totestinstanceof Integer)返回(“Integer”);
否则如果(ToTest instanceof Double)返回(“Double”);
如果(浮动的测试实例)返回(“浮动”);
else if(字符串的测试实例)返回(“字符串”);
else if(ToTest.getClass().isArray())返回(“数组”);
否则返回(“不确定”);
}
}
我这样调用它,其中myArray部分只是将一个数组放入callFunction.tester()中进行测试

public class Generics {
public static void main(String[] args) {
    int [] myArray = new int [10];

    TypeTester<Integer> callFunction = new TypeTester<Integer>();
    System.out.println(callFunction.tester(myArray));
}
}
公共类
public class Generics {
public static void main(String[] args) {
    int [] myArray = new int [10];

    TypeTester<Integer> callFunction = new TypeTester<Integer>();
    System.out.println(callFunction.tester(myArray));
}
}
public static void chkType(Object var){     
        String type = var.getClass().toString();
        System.out.println(type.substring(16));     
        //assertEquals(type,"class java.lang.Boolean");
        //assertEquals(type,"class java.lang.Character");
        //assertEquals(type,"class java.lang.Integer");
        //assertEquals(type,"class java.lang.Double");      
    }