我发现,在<代码>null<>/code>上工作的Im代码中,有了一个插栏:。 目标代码>,因为它被采用某种方法。
为什么这样做?
我知道。 问题涉及超载方法,并使用斜体确定使用何种方法。
但是,如果不进行投放,则会拖上超负荷的方法,其参数为<代码>。 如果将这种方法称作无效理由,则将目标选用在方法的任何其他配对版本上? 因此,所投的成果是什么?
我发现,在<代码>null<>/code>上工作的Im代码中,有了一个插栏:。 目标代码>,因为它被采用某种方法。
为什么这样做?
我知道。 问题涉及超载方法,并使用斜体确定使用何种方法。
但是,如果不进行投放,则会拖上超负荷的方法,其参数为<代码>。 如果将这种方法称作无效理由,则将目标选用在方法的任何其他配对版本上? 因此,所投的成果是什么?
If the cast where not performed, then the most specific version would be chosen.
>(目标
)。 因此,如果可以采用这两种方法,则将采用<代码>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
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...