Java method dispatch with null argument

前端 未结 4 1364
孤独总比滥情好
孤独总比滥情好 2020-11-27 08:38

Why does it (apparently) make a difference whether I pass null as an argument directly, or pass an Object that I assigned the value

相关标签:
4条回答
  • 2020-11-27 08:59

    Because the second commented out invocation with null is ambiguous to the compiler. The literal null could be a string or an object. Whereas the assigned value has a definite type. You need to cast the null, e.g. test.foo((String)null) to remove the ambiguity.

    0 讨论(0)
  • 2020-11-27 09:03

    Has anyone tried the example ???

    With 1.6.0 foo(null) is dispatched to the most specific method applicable which is foo(String)...

    If you add a new method say foo(Integer) the compiler cannot choose the most specific applicable method and shows an error.

    -Patrick

    0 讨论(0)
  • 2020-11-27 09:08

    Which version of Java are you using? With 1.6.0_11 the code (pasted below) compiles and runs.

    I am sure its obvious why foo(testVal) goes to foo(Object).

    The reason why foo(null) goes to foo(String) is a little complex. The constant null is of type nulltype, which is a subtype of all types. So, this nulltype extends String, which extends Object.

    When you call foo(null) compiler looks for the overloaded method with most specific type. Since String is more specific then Object that is the method that gets called.

    If you had another overload that was as specific as String, say foo(Integer) then you would get a ambiguous overload error.

    class NullType {
    
      public static final void main(final String[] args) {
        foo();
      }
    
      static void foo()
      {
        Object testVal = null;
        foo(testVal);    // dispatched to foo(Object)
        foo(null);    // compilation problem -> "The method foo(String) is ambiguous"   
      }
    
      public static void foo(String arg) { // More-specific
        System.out.println("foo(String)");
      }
    
      public static void foo(Object arg) { // Generic
        System.out.println("foo(Object)");
      }
    
    }
    
    0 讨论(0)
  • 2020-11-27 09:15

    Sorry to use an answer, for a comment, but I need to post code that won't fit in comment.

    @Yang, I am also able to compile and run the following. Can you post a complete code that compiles with one line commented such that if I uncomment that line it won't compile?

    class NullType {
    
      public static final void main(final String[] args) {
        foo();
        new Test().bar(new Test());
      }
    
      static void foo()
      {
        Object testVal = null;
        foo(testVal);    // dispatched to foo(Object)
        // foo(null);    // compilation problem -> "The method foo(String) is ambiguous"   
      }
    
      public static void foo(String arg) { // More-specific
        System.out.println("foo(String)");
      }
    
      public static void foo(Integer arg) { // More-specific
        System.out.println("foo(Integer)");
      }
    
      public static void foo(Object arg) { // Generic
        System.out.println("foo(Object)");
      }
    
    
    }
    
    
    class Test
    {
      void bar(Test test)
      {
        Object testVal = null;
        test.foo(testVal);    // dispatched to foo(Object)
        test.foo(null);    // compilation problem -> "The method foo(String) is ambiguous"   
      }
    
      public void foo(String arg) { // More-specific
        System.out.println("foo(String)");
      }
    
      public void foo(Object arg) { // Generic
        System.out.println("foo(Object)");
      }
    }
    
    0 讨论(0)
提交回复
热议问题