【问题标题】:How to determine the primitive type of a primitive variable?如何确定原始变量的原始类型?
【发布时间】:2012-09-03 21:33:37
【问题描述】:

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

instanceof 似乎只适用于类类型。

【问题讨论】:

  • 您是否正在寻找代表intlong 等的Class
  • 你不能在不知道原始数据类型的情况下拥有原始数据类型。必须将其装箱为 Number 类型才能让您不知道它,在这种情况下您可以使用 instanceof
  • @Thor84no 是的,你可以通过反射
  • @Bohemian 我认为反射也自动使用包装类,虽然我没有测试过。

标签: java primitive-types typeof


【解决方案1】:

尝试以下方法:

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

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

【讨论】:

  • 不幸的是,这不是原始类型。这只是盒装类型。
  • @dtc 它可以传递地工作,我还没有找到它不起作用的实例。它甚至适用于布尔值。那么,如果对象每次都能正确地装箱,那有什么区别呢?
【解决方案2】:

有一种不需要隐式装箱的简单方法,因此您不会混淆原语和它们的包装器。您不能将isInstance 用于原始类型——例如调用Integer.TYPE.isInstance(5)Integer.TYPE 相当于int.class)将返回false,因为5 被自动装箱为Integer

获得你想要的东西的最简单方法(注意 - 它在技术上是在编译时为原语完成的,但它仍然需要评估参数)是通过重载。见我的ideone paste

...

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

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

...

可以这样使用,例如:

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

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

【讨论】:

    【解决方案3】:

    您可以使用以下类。

    class TypeResolver
    {
        public static String Long = "long";
        public static String Int = "int";
        public static String Float = "float";
        public static String Double = "double";
        public static String Char = "char";
        public static String Boolean = "boolean";
        public static String Short = "short";
        public static String Byte = "byte";
    
        public static void main(String[] args)
        {
            //all true
            TypeResolver resolver = new TypeResolver();
            System.out.println(resolver.getType(1) == TypeResolver.Int); 
            System.out.println(resolver.getType(1f) == TypeResolver.Float); 
            System.out.println(resolver.getType(1.0) == TypeResolver.Double);
            System.out.println(resolver.getType('a') == TypeResolver.Char); 
            System.out.println(resolver.getType((short) 1) == TypeResolver.Short); 
            System.out.println(resolver.getType((long) 1000) == TypeResolver.Long);
            System.out.println(resolver.getType(false) == TypeResolver.Boolean); 
            System.out.println(resolver.getType((byte) 2) == TypeResolver.Byte);
        }
    
        public String getType(int x)
        {
            return TypeResolver.Int;
        }
    
        public String getType(byte x)
        {
            return TypeResolver.Byte;
        }
    
        public String getType(float x)
        {
            return TypeResolver.Float;
        }
    
        public String getType(double x)
        {
            return TypeResolver.Double;
        }
    
        public String getType(boolean x)
        {
            return TypeResolver.Boolean;
        }
    
        public String getType(short x)
        {
            return TypeResolver.Short;
        }
    
        public String getType(long x)
        {
            return TypeResolver.Long;
        }
    
        public String getType(char x)
        {
            return TypeResolver.Char;
        }
    }
    

    【讨论】:

    • 考虑到您正在使用函数重载来为您完成任务,这非常巧妙。
    • 只是指出缺点:它需要持续注意新添加的内容 - 因此不会自动覆盖“新”项目。 - 维护编码语言基础设施服务是在语言开发人员领域解决的事情。因此,开发语言本身不会对现有项目代码产生额外影响。通过这样做,任何使用相关环境特性的代码都将在接下来编译/解释时立即从这些特性中受益。
    • 这与obataku 8 年前给出的答案基本相同,只是obataku 重用Class&lt;&gt;.TYPE 而不是重新定义所有类型名称。所以实际上这个答案并不那么优雅。
    【解决方案4】:

    有两种方法可以用来确定 Primitive 类型的类型。

    package com.company;
    
    public class Testing {
    public static void main(String[] args) {
        int x;
        x=0;
        // the first method 
        System.out.println(((Object)x).getClass().getName());
        if (((Object)x).getClass().getName()=="java.lang.Integer")
            System.out.println("i am int");
       // the second method it will either return true or false
        System.out.println(Integer.class.isInstance(x));
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      相关资源
      最近更新 更多