English 中文(简体)
为什么不反对?
原标题:Why cast null to Object?
  • 时间:2011-04-27 14:57:06
  •  标签:
  • java
  • null

我发现,在<代码>null<>/code>上工作的Im代码中,有了一个插栏:。 目标,因为它被采用某种方法。

为什么这样做?

我知道。 问题涉及超载方法,并使用斜体确定使用何种方法。

但是,如果不进行投放,则会拖上超负荷的方法,其参数为<代码>。 如果将这种方法称作无效理由,则将目标选用在方法的任何其他配对版本上? 因此,所投的成果是什么?

最佳回答

If the cast where not performed, then the most specific version would be chosen.

类型<代码>String或类型>(目标)。 因此,如果可以采用这两种方法,则将采用<代码>String方法。

If you have methods with Object, Integer and String then calling that with null (and no cast) would give a compilation error because Integer and String are both valid and equally specific (i.e. none is a specialization of the other). In that case you would have to cast null to specify which method to call.

问题回答

“最低限值/代码”方法在所有“适用方法”中始终是“最具体的”方法。 这正是汇编者为什么要选择。

页: 1

String.valueOf(null);

然后,汇编者可选择两种“适用方法”。 您实际上称得更具体的方法

String.valueOf((char[]) null);

将向您提供<代码>NullPointerException。 为了使用其他方法,撰写

String.valueOf((Object) null);

In this case, you remain with only one "applicable method", so you don t have the problem of a different, overloaded method being "more specific".

Though previous answers already explains what happens if you cast null to Object vs. if you don t cast null to Object, but I would still like to add few missing points.

So in Java, arrays are objects which means they can be assigned to Object type i.e. if you do new char[0].getClass().getSuperclass() it gives java.lang.Object and therefore in case when null is not explicitly cast compiler chooses valueOf(char[]) over valueOf(Object) as the most applicable method.

但是,如果存在另一个超负荷的方法,即接受一个接口类型段(remember 界面不延伸 t)。 因此,目标类别也造成大多数特定方法选择“”的模糊性,例如价值Of(CharSequence)的模糊性,从而导致时间错误(即提及价值Of含糊不清)的汇编,因为汇编者可以选择最适用的方法。

So the bottom line is avoid passing raw null as arguments to methods rather always cast them to param type of the method being called. :)

"But if the cast were not performed, wouldn t an overloaded method with a parameter typed as Object be chosen over any other matching version of the method if the method is called with a null argument? "

页: 1





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签