在java中 如何获取变量的类型?

[复制链接]
查看11 | 回复4 | 2015-10-23 20:47:37 | 显示全部楼层 |阅读模式
java中获取变量的类型可以使用每个类型的getClass()方法来获得,示例如下:class Test { public static void main(String[] args) { int i=1; //int类型变量System.out.println(getType(i)); //打印变量类型为int} public static String getType(Object o){ //获取变量类型方法return o.getClass().toString(); //使用int类型的getClass()方法} }...
回复

使用道具 举报

千问 | 2015-10-23 20:47:37 | 显示全部楼层
你得到java.lang.String 让后用subString截取最后点的字符串进行比较!!...
回复

使用道具 举报

千问 | 2015-10-23 20:47:37 | 显示全部楼层
public class Test15 {public static void main(String[] args) {int i=1;Integer i1 = 2;double d = 1.8;long l = 76;boolean b = false;System.out.println(getType(i));System.out...
回复

使用道具 举报

千问 | 2015-10-23 20:47:37 | 显示全部楼层
无法获取,只能使用instanceof判断是不是指定类型如 “abc” instanceOf String返回ture...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行