Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法检查java原语变量的实例_Java_Variables_Primitive Types - Fatal编程技术网

有没有办法检查java原语变量的实例

有没有办法检查java原语变量的实例,java,variables,primitive-types,Java,Variables,Primitive Types,通过使用instanceof操作符,我们可以知道对象引用是一种测试。但是是否有任何操作符来检查基元类型。例如: byte b = 10; 现在,如果我只考虑值 10 。我有没有办法发现它被声明为字节?局部变量 假设您指的是局部变量,则无论何时作为对象传递,原语都会自动按照其包装器类型进行包装,在本例中为java.lang.Byte。使用反射引用局部变量是不可能的,所以不能区分字节和字节、整数和整数等 Object bytePrimitive = (byte) 10; System.out.p

通过使用instanceof操作符,我们可以知道对象引用是一种测试。但是是否有任何操作符来检查基元类型。例如:

byte b = 10;
现在,如果我只考虑值<代码> 10 <代码>。我有没有办法发现它被声明为字节?

局部变量 假设您指的是局部变量,则无论何时作为对象传递,原语都会自动按照其包装器类型进行包装,在本例中为java.lang.Byte。使用反射引用局部变量是不可能的,所以不能区分字节和字节、整数和整数等

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());
    }

}
注意:字节是最终的,所以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

等等。

如果你真的想玩文字…

    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");            
    }

我知道这听起来很疯狂。为什么我想知道数据类型,而我可以在不知道变量的数据类型的情况下工作,但我问这个问题只是出于好奇,在将它用于局部变量或对象字段之前,我从未遇到过知道声明的基元类型的情况??它可以是局部变量或对象字段,但它必须不是基元wrappersBecause
ref instanceof Byte
过于主流:p感谢解释,但局部变量指的是块内的变量,而不是不稳定或静态变量“isInstance”方法在哪里?该方法是类
class
的成员。isInstance接受“Object”类作为参数我们如何为其提供原语Java自动将原语(例如int)转换为包装的原语对象(例如Integer),反之亦然,如果赋值或方法参数需要这样做的话。这个功能叫做自动装箱/取消装箱。好的,我知道装箱,但它是关于将原语转换为包装器的,反之亦然,它不会将原语转换为对象,所以我们不能直接将原语分配给对象,但我们可以将包装器分配给对象。没有包装器,我想问一下原语,这是一个很好的例子
    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");            
    }