在其他语言编程,都可以直接调用系统函数输出数据类型,java只能自己实现;

第一种可以运用getClass():

使用getClass()是不用引用工具包的,直接调用

public class Text1{
public static String getType(Object obj){
return obj.getClass().toString();
}
public static void main(String[] args){
int i=1;
char c='1';
float f=1.1f;
System.out.println(getType(i));
System.out.println(getType(c));
System.out.println(getType(f));
}
 }

第二种是使用函数的重载:

public class GetClass {
public static void main(String[] args){
byte b=1;
char c='a';
short s=1;
int i=1;
float f=1.1f;
double d=1.0;
String str="aber";
System.out.println(getType(b));
System.out.println(getType(c));
System.out.println(getType(s));
System.out.println(getType(i));
System.out.println(getType(f));
System.out.println(getType(d));
System.out.println(getType(str));
}
static String getType(byte obj){
return "byte";
}
static String getType(char obj){
return "char";
}
static String getType(short obj){
return "short";
}
static String getType(int obj){
return "int";
}
static String getType(float obj){
return "float";
}
static String getType(double obj){
return "double";
}
static String getType(String obj){
return "String";
}
 }