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

  • Home
  • SEARCH
  • 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 628219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:36:46+00:00 2026-05-13T19:36:46+00:00

Why does the following program throw an exception? public class MainClass{ public static void

  • 0

Why does the following program throw an exception?

public class MainClass{
  public static void main(String[] argv){
       callMethod(2);
  }
  public static void callMethod(Integer... i){
       System.out.println("Wrapper");
  }
  public static void callMethod(int... i){
       System.out.println("Primitive");
  }

}

The method callMethod(Integer[]) is ambiguous for the type MainClass

OK, I can see that either of the two methods will work (if the other is commented out), but I also know that there is hierarchy on what happens if a primitive is not exactly matched by the type of the input of a method.

The first thing that is tried is to widen the primitive. So, if there was a third method:

      public static void callMethod(long i){
       System.out.println("long");
      }

The code would print long

The second thing is to box the primitive. So if there was a method taking an Integer, that would be the one invoked.

The third priority is the var-args.

Based on the above priority, I would expect that the second priotity be the case. I would expect the int to be wrapped into an Integer and the (Integer…) would be invoked. But of course this does not happen. Instead, the exception is thrown.

Does anyone see and can explain why the prioritization does not apply in this example?

Cheers!

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-05-13T19:36:46+00:00Added an answer on May 13, 2026 at 7:36 pm

    You are right that widening comes before boxing, which in turn comes before var-args.

    But you seem to be treating the first method as callMethod(Integer i), not callMethod(Integer... i). Since both methods use var-args, there’s a priority tie. That is, neither one meets the criteria for boxing alone, but both meet the criteria for var-args.

    Remember that it’s illegal to widen, then box (although I did some research before posting this answer and found that it IS legal to box, then widen). Similarly, you won’t get box, then var-args behavior; the compiler skips right to the var-args step and sees two methods that take var-args.

    EDIT: I should clarify that you WILL get box-then-var-args behavior if there’s no ambiguity. In other words, if there was only one callMethod(), and it took Integer... i, you would get “Wrapper.”

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

Sidebar

Related Questions

In the following program. public class SerialExample { public static void main(String args[]) throws
When debugging the following console program: class Program { static void Main(string[] args) {
Take this code: using System; namespace OddThrow { class Program { static void Main(string[]
Why does the following program crash? I have a base class whose destructor is
Why does the following program segfault? int main() { main(); } Even though it
What does the colon mean in the following Perl program? MAIN: { print Hello\n;
The following program : public class SimpleCounter extends HttpServlet { int counter=0; @Override protected
Why does the following code throw an exception? for (int i = 0; i
When answering another question, I realized that the following program does not quite do
In the following program, why does hitting the a key print hello, world while

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.