Java 使用 null 对象获取对象的类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20378132/
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
Get type of an object with an object of null?
提问by Monte Carlo
The following code does compile, but why do I get a run time exception?
下面的代码可以编译,但为什么会出现运行时异常?
String b = null;
System.out.println(b.getClass());
Error I get is
我得到的错误是
java.lang.NullPointerException
How can I get the type of the object even if it's set to null?
即使将对象设置为 null,如何获取对象的类型?
EditI realize there is no object present, but there still is an object b of type String. Even if it holds no object, it still has a type. How do I get at the type of an object, regardless of if it holds an object or if it does not.
编辑我意识到没有对象存在,但仍然有一个字符串类型的对象 b。即使它没有对象,它仍然有一个类型。我如何获得一个对象的类型,无论它是否持有一个对象。
回答by SLaks
There is no object here, and null
has no type.
You can't do that.
这里没有对象,null
也没有类型。
你不能那样做。
回答by John Kugelman
Null references don't have a type. There's no object being pointed to, so you can't call .getClass()
on the non-existent object.
空引用没有类型。没有对象被指向,所以你不能调用.getClass()
不存在的对象。
If you're looking for the compile-time type, you can write String.class
instead.
如果您正在寻找编译时类型,则可以String.class
改为编写。
回答by Damian Leszczyński - Vash
To answer to your question you must understand what have you written.
要回答您的问题,您必须了解您所写的内容。
String b = null;
can be translated to. Reserve in memory space for a String class and fill it with null.
String b = null;
可以翻译成。为 String 类保留内存空间并用 null 填充它。
So there is not object in that statement. That is why you get NullPointerException.
所以该语句中没有对象。这就是您得到 NullPointerException 的原因。
To retrieve the type of such declaration you can use reflection mechanism but it only apply to class members.
要检索此类声明的类型,您可以使用反射机制,但它仅适用于类成员。
回答by Sotirios Delimanolis
When you have
当你有
String b = null;
what you actually have is a variable of reference type String
that is referencing null
. You cannot dereference null
to invoke a method.
您实际拥有的是一个引用类型的变量,String
它正在引用null
. 您不能取消引用null
来调用方法。
With local variables you cannot do what you are asking.
使用局部变量,你不能做你所要求的。
With member variables, you can use reflection to find the declared type of the field.
使用成员变量,您可以使用反射来查找字段的声明类型。
Field field = YourClass.class.getDeclaredField("b");
Class<?> clazz = field.getType(); // Class object for java.lang.String
回答by Jesper
A variableis not the same as an object. A variable is a referenceto an object.
甲变量是不一样的物件。变量是对对象的引用。
You get a NullPointerException
if you try to dereference a null
reference (i.e., if you try to call a method or access a member variable through a null
reference).
你得到了NullPointerException
,如果你尝试取消引用null
引用(即,如果您尝试调用一个方法或访问通过一个成员变量null
引用)。
When a variable is null
, it means it refers to no object. Ofcourse you can't get the type of "no object".
当变量为 时null
,表示它不引用任何对象。当然,您无法获得“无对象”的类型。
回答by DanK
Everyone else has appropriately answered that there is no type as there is no object but if I'm reading this correctly, I think what you are really asking is about how to check the type that your variable has been assigned.
其他人都适当地回答说没有类型,因为没有对象,但是如果我没有正确阅读,我认为您真正要问的是如何检查已分配变量的类型。
For that I would refer to this link: How to check type of variable in Java?
为此,我会参考此链接: How to check type of variable in Java?
回答by Matthias Ronge
As the above answas state correcty, this is not possible. The closest I came up with is this:
由于上述答案是正确的,这是不可能的。我想出的最接近的是这个:
<T extends Object> String typeAndValue(Class<T> type, T value) {
return type.getClass.getSimpleName() + " is: " + String.valueOf(value);
}
You need to explicitly pass the type, but you cannot pass anything different than a suitable type (still may be supperclass) because it would not compile.
您需要显式传递类型,但不能传递与合适类型不同的任何内容(仍然可能是超类),因为它不会编译。