java 有没有办法检查instanceof原语变量java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27400919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is there a way to check instanceof primitives variables java
提问by Mateen
We can know object reference is-a test by using instanceof operator. But is there any operator to check primitive types. For example:
我们可以通过使用 instanceof 运算符知道对象引用 is-a 测试。但是是否有任何运算符来检查原始类型。例如:
byte b = 10;
Now if I only consider the value 10
. Is there any way I could find out that it was declared as a byte?
现在如果我只考虑价值10
。有什么办法可以找出它被声明为一个字节吗?
采纳答案by Adam
Local variables
局部变量
Assuming you mean by local variables the primitive will always be automatically wrapped by its wrapper type whenever passed as an object, java.lang.Byte in this case. It's impossible to refer to local variables using reflection so you cannot differentiate between Byte and byte or Integer and int etc.
假设您的意思是局部变量,只要作为对象传递,原语将始终由其包装器类型自动包装,在本例中为 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()));
Fields
字段
However, if you're talking about fields in classes, then things are different as you canget a handle on actual declared type. You can then use java.lang.Class.isPrimitive() as expected and the type will be byte.class.
但是,如果你在谈论的类字段,然后情况就不同了,你可以在实际的声明类型获得一个句柄。然后您可以按预期使用 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());
}
}
回答by Gren
If you really want to play with literals...
如果你真的想玩文字......
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");
}
回答by javaHunter
byte b = 10;
Object B= b;
if (B.getClass() == Byte.class) {
System.out.println("Its a Byte");
}
Note: Byte is final, so instanceof is equivalent to class equality.
注意:Byte 是 final,所以 instanceof 等价于类相等。
Now if you try:
现在,如果您尝试:
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
etc...
等等...