Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 114581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:53:01+00:00 2026-05-11T02:53:01+00:00

I noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The

  • 0

I noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The simplest example appears to be this:

public class Test {     static void f(Object a, boolean b) {}     static void f(Object a, Object b) {}      static void m(int a, boolean b) { f(a,b); } } 

When compiled, it causes the following error:

Test.java:5: reference to f is ambiguous, both method     f(java.lang.Object,boolean) in Test and method     f(java.lang.Object,java.lang.Object) in Test match  static void m(int a, boolean b) { f(a, b); }                                   ^ 

The fix to this error is trivial: just use explicit auto-boxing:

static void m(int a, boolean b) { f((Object)a, b); } 

Which correctly calls the first overload as expected.

So why did the overload resolution fail? Why didn’t the compiler auto-box the first argument, and accept the second argument normally? Why did I have to request auto-boxing explicitly?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:53:01+00:00Added an answer on May 11, 2026 at 2:53 am

    When you cast the first argument to Object yourself, the compiler will match the method without using autoboxing (JLS3 15.12.2):

    The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

    If you don’t cast it explicitly, it will go to the second phase of trying to find a matching method, allowing autoboxing, and then it is indeed ambiguous, because your second argument can be matched by boolean or Object.

    The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation.

    Why, in the second phase, doesn’t the compiler choose the second method because no autoboxing of the boolean argument is necessary? Because after it has found the two matching methods, only subtype conversion is used to determine the most specific method of the two, regardless of any boxing or unboxing that took place to match them in the first place (§15.12.2.5).

    Also: the compiler can’t always choose the most specific method based on the number of auto(un)boxing needed. It can still result in ambiguous cases. For example, this is still ambiguous:

    public class Test {     static void f(Object a, boolean b) {}     static void f(int a, Object b) {}      static void m(int a, boolean b) { f(a, b); } // ambiguous } 

    Remember that the algorithm for choosing a matching method (compile-time step 2) is fixed and described in the JLS. Once in phase 2 there is no selective autoboxing or unboxing. The compiler will locate all the methods that are accessible (both methods in these cases) and applicable (again the two methods), and only then chooses the most specific one without looking at boxing/unboxing, which is ambiguous here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed today that I can't use * to pass width or precision arguments
I noticed today that SO uses magic URLs in the form. For example, a
I encountered an interesting thing today that I have never noticed before. It appears
I am relatively new to C#, and I noticed something interesting today that I
Today I noticed that now when I Ctrl+/ one or more lines in Eclipse
I ran my app through Instruments today and noticed that an excessively high amount
Just today I noticed on our Wordpress blog that the Facebook iframe is showing
Today I have noticed that the order in which the $lt and $gt operators
Today I installed Visual Studio 2010 Ultimate - RTM. One item that I noticed
I noticed today that delete will not work in chrome when this popup blocker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.