【问题标题】:is there a way to check instanceof primitives variables java有没有办法检查instanceof原语变量java
【发布时间】:2014-12-10 12:22:59
【问题描述】:

我们可以通过使用instanceof操作符知道对象引用是一个测试。但是是否有任何运算符来检查原始类型。例如:

byte b = 10;

现在,如果我只考虑值 10。有什么办法可以发现它被声明为一个字节?

【问题讨论】:

  • 我知道这听起来很疯狂。当我可以在不知道变量的数据类型的情况下工作时,为什么我想知道数据类型,但我只是出于好奇而问这个问题,我从来没有遇到过在使用之前知道声明的原始类型的情况
  • 这是用于局部变量还是对象字段??
  • 它可以是局部变量或对象字段,但它必须是原始的而不是包装器

标签: java variables primitive-types


【解决方案1】:

如果你真的想玩文字...

    if(Byte.class.isInstance(10)) {
        System.out.println("I am an instance of Byte");         
    }
    if(Integer.class.isInstance(10)) {
        System.out.println("Ups, I can also act as an instance of Integer");            
    }
    if(false == Float.class.isInstance(10)) {
        System.out.println("At least I am not a float or double!");         
    }
    if(false == Byte.class.isInstance(1000)) {
        System.out.println("I am too big to be a byte");            
    }

【讨论】:

  • “isInstance”方法在哪里?
  • 该方法是类Class的成员。
  • isInstance 以“Object”类为参数,我们如何为它提供原语
  • 如果赋值或方法参数需要,Java 会自动将原始(例如 int)转换为包装的原始对象(例如 Integer),反之亦然。该功能称为自动装箱/拆箱。
  • 好吧,我知道拳击,但它是关于将原语转换为包装器,反之亦然,它不会将原语转换为对象,所以我们不能直接将原语分配给对象,但我们可以将包装器分配给对象
【解决方案2】:

局部变量

假设您的意思是局部变量,无论何时作为对象传递,原语将始终由其包装器类型自动包装,在这种情况下为 java.lang.Byte。使用反射无法引用局部变量,因此无法区分 Byte 和 byte 或 Integer 和 int 等。

Object bytePrimitive = (byte) 10;

System.out.println("is a Byte ?   " + (bytePrimitive instanceof Byte));
System.out.println("Check class = " + (bytePrimitive.getClass()));

// false because class in this case becomes Byte, not byte.
System.out.println("Primitive = " + (bytePrimitive .getClass().isPrimitive()));

字段

但是,如果您谈论的是类中的字段,那么情况就不同了,因为您可以处理实际声明的类型。然后,您可以按预期使用 java.lang.Class.isPrimitive(),类型将为 byte.class。

public class PrimitiveMadness {
    static byte bytePrimitiveField;
    static Byte byteWrapperField;

    public static void main(String[] args) throws Exception {
        System.out.println("Field type  =     " + PrimitiveMadness.class.getDeclaredField("bytePrimitiveField").getType());
        System.out.println("Is a byte   =     " + (PrimitiveMadness.class.getDeclaredField("bytePrimitiveField").getType() == byte.class));
        System.out.println("Is a primitive? = " + PrimitiveMadness.class.getDeclaredField("bytePrimitiveField").getType().isPrimitive());
        System.out.println("Wrapper field   = " + PrimitiveMadness.class.getDeclaredField("byteWrapperField").getType());
    }

}

【讨论】:

  • 因为ref instanceof Byte太主流了:P
  • 感谢您的解释,但我所说的局部变量是指块内的变量,而不是实例或静态变量
【解决方案3】:
byte b = 10;
Object B= b;
 if (B.getClass() == Byte.class) {
  System.out.println("Its a Byte");
 }

注意:字节是final,所以instanceof等价于类。

现在如果你尝试:

Object ref = 10;
System.out.println(ref.getClass()); //class java.lang.Integer

Object ref = 10.0;
System.out.println(ref.getClass()); //class java.lang.Double

Object ref = 10L;
System.out.println(ref.getClass()); //class java.lang.Long

等等……

【讨论】:

  • 老兄没有包装我想问关于原语的问题反正这是一个很好的例子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-12
  • 2012-03-25
  • 1970-01-01
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多