在Java中重载方法中使用null [英] Using null in overloaded methods in Java

查看:187
本文介绍了在Java中重载方法中使用null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

NULL参数的方法重载

以下代码编译和很好。

The following code compiles and goes fine.

public class Main
{
    public void temp(Object o)
    {
        System.out.println("The method with the receiving parameter of type Object has been invoked.");
    }

    public void temp(String s)
    {
        System.out.println("The method with the receiving parameter of type String has been invoked.");
    }

    public void temp(int i)
    {
        System.out.println("The method with the receiving parameter of type int has been invoked.");
    }

    public static void main(String[] args)
    {
        Main main=new Main();
        main.temp(null);
    }
}

在此代码中,要调用的方法是接受类型参数的一个

In this code, the method to be invoked is the one that accepts the parameter of type String

docs 说。


如果多个成员方法都可访问且适用于
方法调用,则必须选择一个为运行时方法调度提供
描述符。 Java编程
语言使用选择最具体方法的规则。

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

但我不明白何时一个接受原语 int 参数的代码中的方法被修改为接受包装类型的参数 Integer 例如,

but I don't understand when one of the methods in the code that accepts the parameter of the primitive int is modified to accept the parameter of the wrapper type Integer such as,

public void temp(Integer i)
{
    System.out.println("The method with the receiving parameter of type Integer has been invoked.");
}

发出编译时错误。


对temp的引用是不明确的,
methodoverloadingpkg.Main中的方法temp(java.lang.String)和$中的方法temp(java.lang.Integer) b $ b methodoverloadingpkg.Main match

reference to temp is ambiguous, both method temp(java.lang.String) in methodoverloadingpkg.Main and method temp(java.lang.Integer) in methodoverloadingpkg.Main match

在这个特定场景中,为什么使用原始数据类型重载方法是合法的它的相应包装类型似乎不是这样吗?

In this particular scenario, why is it legal to overload a method with a primitive data type which however doesn't appear to be the case with its corresponding wrapper type?

推荐答案

如果有人问你是什么更专业的字符串或对象,你会说什么?显然是字符串,对吧?

If you were asked what is more specialized "String" or "Object", what would you say? Evidently "String", right?

如果有人问你:什么是更专业的字符串或整数?没有答案,它们都是对象的正交特化,你如何在它们之间做出选择?那么你必须明确你想要哪一个。例如,通过转换空引用:

If you were asked: what is more specialized "String" or "Integer"? There is no answer, they are both orthogonal specializations of an object, how can you choose between them? Then you must be explicit regarding which one you want. For instance by casting your null reference:

question.method((String)null)

使用基元类型时,没有那个问题,因为null是一种引用类型,不能与基本类型冲突。但是当你使用引用类型时,null可以引用String或Integer(因为null可以转换为任何引用类型)。

When you use primitive types you do not have that problem because "null" is a reference type and cannot conflict with primitive types. But when you use reference types "null" could refer to either String or Integer (since null can be cast to any reference type).

请参阅我在上述评论中发布的其他问题中的答案,以获取更多更深入的详细信息,甚至还有一些来自JLS。

See the answer in the other question that I posted in the comments above for further and deeper details and even a few quotes from the JLS.

这篇关于在Java中重载方法中使用null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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