类型为null的引用类型? [英] Type of reference type with value null?

查看:217
本文介绍了类型为null的引用类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据: http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html
4.5.2引用类型的变量
引用类型可以包含null引用.

According to: http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html
4.5.2 Variables of Reference Type
A reference type can hold a null reference.

当赋值为null时是否可以检索引用类型的声明类型?

Is it possible to retrieve the declared type of the reference type when the assigned value is null?

具体来说,在使用反射的方法中,我希望该方法是null安全的,并且可以作用于原始声明的类型(尽管我知道以下代码段不起作用),例如:

Specifically, in a method that uses reflection, I wish the method to be null-safe and act on the original declared type (though I know the following code snippet doesn't work), for instance:

String referenceType = null;
MyReflectionClass.reflectionMethod(referenceType);
...
public static void reflectionMethod(Object referenceType) {
   Class<?> declaredType = referenceType.getClass();
}

如有必要,我不会反对使用泛型将类型为T而不是Object作为声明的参数类型.

I would not be averse to using generics to have type T instead of Object as the declared parameter type, if necessary.

:我知道.getClass()适用于实例,而不适用于声明的类型.我想知道是否有可能要问引用为它的声明类型.由于类层次结构是静态的,因此获取该信息应该没有问题.

I know that .getClass() works on the instance, not the declared type. I was wondering if it was possible to ask the reference for it's declared type. Since class hierarchies are static, there should be no problem to get that information.

Edit2 :这里的情况很清楚:是Java 通过引用"或按值传递"?
Java只是按值传递,因此,即使使用了引用类型,也总是将其当作传递值(对象实例)来处理(即使内部仅传递对象指针).这意味着Java实际上没有引用类型知道它的类型(至少就程序员而言),而所有这些都在值实例中.
因此,不可能确定任何null值的类型.

Here, the situation is made clear: Is Java "pass-by-reference" or "pass-by-value"?
Java is only pass-by-value, so even though a reference type is used, it is always handled as if the value (object instance) is passed (even though the internals only pass an object pointer). This means that Java doesn't actually have a reference type that knows about it's type (at least as far as the programmer is concerned), it's all in the value instances.
Therefore it is impossible to determine the type of any null value.

推荐答案

一个字,没有.但是,尝试查找null引用的成员没有任何意义,因为null表示不存在任何内容.您最好不使用null作为输入,最好抛出NullPointerException.

In a single word, no. It doesn't make a whole lot of sense to try to find the members of a null reference, though, since null implies the absence of anything. You're probably better off just dis-allowing null as an input, probably by throwing a NullPointerException.

如果必须处理null,最好的方法可能是显式传递类引用(甚至更好的是直接对所讨论的方法进行引用).

If you must handle null, perhaps the best way is to explicitly pass the class reference (or even better, a direct reference to the method in question).

public static void reflectionMethod(Object param, Class<?> referenceType) // null is dis-allowed for referenceType
{
    // ... no need to try to back-track the type from an object
}

如果您只能获得空值,那么很难做出(除非施加其他约束),这要比非常幸运的猜测要好得多.

If you can only ever get the null value, it's difficult (impossible unless there are other constraints imposed) to do much better than having a very lucky guess.

这篇关于类型为null的引用类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆