如何确定原始变量的原始类型? [英] How to determine the primitive type of a primitive variable?

查看:162
本文介绍了如何确定原始变量的原始类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中是否有类似typeof的函数返回原始数据类型(PDT)变量的类型或操作数PDT的表达式?

Is there a "typeof" like function in Java that returns the type of a primitive data type (PDT) variable or an expression of operands PDTs?

instanceof 似乎只适用于类类型。

instanceof seems to work for class types only.

推荐答案

请尝试以下操作:

int i = 20;
float f = 20.2f;
System.out.println(((Object)i).getClass().getName());
System.out.println(((Object)f).getClass().getName());

它将打印:

java.lang.Integer
java.lang.Float

对于 instanceof ,您可以使用其动态对应 Class#isInstance

As for instanceof, you could use its dynamic counterpart Class#isInstance:

Integer.class.isInstance(20);  // true
Integer.class.isInstance(20f); // false
Integer.class.isInstance("s"); // false

这篇关于如何确定原始变量的原始类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆