如何确定原始变量的原始类型?

时间:2021-01-13 16:28:00

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?

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

instanceof seems to work for class types only.

instanceof似乎只适用于类类型。

2 个解决方案

#1


55  

Try the following:

请尝试以下方法:

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

It will print:

它将打印:

java.lang.Integer
java.lang.Float

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

例如,您可以使用其动态对应的Class#isInstance:

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

#2


14  

There's an easy way that doesn't necessitate the implicit boxing, so you won't get confused between primitives and their wrappers. You can't use isInstance for primitive types -- e.g. calling Integer.TYPE.isInstance(5) (Integer.TYPE is equivalent to int.class) will return false as 5 is autoboxed into an Integer before hand.

有一种简单的方法,不需要隐式装箱,所以你不会在原语和它们的包装器之间混淆。您不能将isInstance用于基本类型 - 例如调用Integer.TYPE.isInstance(5)(Integer.TYPE等效于int.class)将返回false,因为5是在事先将其自动装箱到Integer中。

The easiest way to get what you want (note - it's technically done at compile-time for primitives, but it still requires evaluation of the argument) is via overloading. See my ideone paste.

获得所需内容的最简单方法(注意 - 技术上在原型编译时完成,但仍需要评估参数)是通过重载。看我的ideone粘贴。

...

public static Class<Integer> typeof(final int expr) {
  return Integer.TYPE;
}

public static Class<Long> typeof(final long expr) {
  return Long.TYPE;
}

...

This can be used as follows, for example:

这可以如下使用,例如:

System.out.println(typeof(500 * 3 - 2)); /* int */
System.out.println(typeof(50 % 3L)); /* long */

This relies on the compiler's ability to determine the type of the expression and pick the right overload.

这依赖于编译器确定表达式类型并选择正确重载的能力。

#1


55  

Try the following:

请尝试以下方法:

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

It will print:

它将打印:

java.lang.Integer
java.lang.Float

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

例如,您可以使用其动态对应的Class#isInstance:

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

#2


14  

There's an easy way that doesn't necessitate the implicit boxing, so you won't get confused between primitives and their wrappers. You can't use isInstance for primitive types -- e.g. calling Integer.TYPE.isInstance(5) (Integer.TYPE is equivalent to int.class) will return false as 5 is autoboxed into an Integer before hand.

有一种简单的方法,不需要隐式装箱,所以你不会在原语和它们的包装器之间混淆。您不能将isInstance用于基本类型 - 例如调用Integer.TYPE.isInstance(5)(Integer.TYPE等效于int.class)将返回false,因为5是在事先将其自动装箱到Integer中。

The easiest way to get what you want (note - it's technically done at compile-time for primitives, but it still requires evaluation of the argument) is via overloading. See my ideone paste.

获得所需内容的最简单方法(注意 - 技术上在原型编译时完成,但仍需要评估参数)是通过重载。看我的ideone粘贴。

...

public static Class<Integer> typeof(final int expr) {
  return Integer.TYPE;
}

public static Class<Long> typeof(final long expr) {
  return Long.TYPE;
}

...

This can be used as follows, for example:

这可以如下使用,例如:

System.out.println(typeof(500 * 3 - 2)); /* int */
System.out.println(typeof(50 % 3L)); /* long */

This relies on the compiler's ability to determine the type of the expression and pick the right overload.

这依赖于编译器确定表达式类型并选择正确重载的能力。