How to check the data type in JAVA?

//使用java中的getClass()函数查看数据类型/
//该函数返回Class类型,在后面加上toString()函数转换为字符串
public static void main(String[] ags){     
    Integer i=1;          
    System.out.println(i.getClass().toString());   //返回class java.lang.Integer
}

Use the built-in method getClass() of Object in java to return the Class type, and add the toString() function to convert it to a string.

Java Object getClass() method:

Object getClass() method is used to get the runtime object's class of the object.

grammar:

object.getClass()

parameter:

none 

return value:

the class of the returned object  

Since getClass() is called with an object, it is inconvenient to use, we can define a getType() method

public static String getType(Object obj){            
        return obj.getClass().toString();
}
int a=1;
System.out.println(getType(a));  //返回class java.lang.Integer

This allows us to use the built-in data types to call

Supongo que te gusta

Origin blog.csdn.net/HouGOD/article/details/120213094
Recomendado
Clasificación