title: Using null in overloaded methods in Java
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5229809/method-overloading-for-null-parameter">Method Overloading for NULL parameter</a>  </p>
  </blockquote>
  
  
  
  <p>The following code compiles and goes fine.    </p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>In this code, the method to be invoked is the one that accepts the parameter of type <code>String</code></p>
  
  <p>The <a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.12.2.5" rel="nofollow noreferrer">docs</a> say.</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>but I don't understand when one of the methods in the code that accepts the parameter of the primitive <code>int</code> is modified to accept the parameter of the wrapper type <code>Integer</code> such as,</p>
  
  <pre><code>public void temp(Integer i)
  {
      System.out.println("The method with the receiving parameter of type Integer has been invoked.");
  }
  </code></pre>
  
  <p>a compile-time error is issued.</p>
  
  <blockquote>
    <p>reference to temp is ambiguous, both method temp(java.lang.String) in
    methodoverloadingpkg.Main and method temp(java.lang.Integer) in
    methodoverloadingpkg.Main match</p>
  </blockquote>
  
  <p>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?</p>