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 8682487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:47:21+00:00 2026-06-12T21:47:21+00:00

The following code doesn’t compile. package varargspkg; public class Main { public static void

  • 0

The following code doesn’t compile.

package varargspkg;

public class Main {

    public static void test(int... i) {
        for (int t = 0; t < i.length; t++) {
            System.out.println(i[t]);
        }

        System.out.println("int");
    }

    public static void test(float... f) {
        for (int t = 0; t < f.length; t++) {
            System.out.println(f[t]);
        }

        System.out.println("float");
    }

    public static void main(String[] args) {
        test(1, 2);  //Compilation error here quoted as follows.
    }
}

A compile-time error is issued.

reference to test is ambiguous, both method test(int…) in
varargspkg.Main and method test(float…) in varargspkg.Main match

It seems to be obvious because the parameter values in the method call test(1, 2); can be promoted to int as well as float

If anyone or both of the parameters are suffixed by F or f, it compiles.


If we however, represent the receiving parameters in the method signature with respective wrapper types as follows

public static void test(Integer... i) {
    System.out.println("Integer" + Arrays.asList(i));
}

public static void test(Float... f) {
    System.out.println("Float" + Arrays.asList(f));
}

then the call to the method test(1, 2); doesn’t issue any compilation error. The method to be invoked in this case is the one that accepts one Integer varargs parameter (the first one in the preceding snippet).

Why is in this case the error as in the first case not reported? It appears that auto-boxing and automatic type promotion are both applied here. Is auto-boxing applied first so that the error is resolved?

The Oracle docs says,

Generally speaking, you should not overload a varargs method, or it
will be difficult for programmers to figure out which overloading gets
called.

The last sentence in this link. It’s however for the sake of better understanding varargs.

Also to add below code compiles just fine.

public class OverLoading {

    public static void main(String[] args) {
        load(1);
    }

    public static void load(int i) {
        System.out.println("int");
    }

    public static void load(float i) {
        System.out.println("float");
    }
}

EDIT:

The following is the snap shot that indicates the compilation error. I have created a new application therefore the package name is different.

enter image description here

I’m using JDK 6.

  • 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-06-12T21:47:22+00:00Added an answer on June 12, 2026 at 9:47 pm

    You can either Widen or Box but you cannot do both, unless you are boxing and widening to Object (An int to Integer(Boxing) and then Integer to Object(Widening) is legal, since every class is a subclass of Object, so it is possible for Integer to be passed to Object parameter)

    Similarly an int to Number is also legal (int -> Integer -> Number)
    Since Number is the super class of Integer it is possible.

    Let’s see this in your example:

    public static void test(Integer...i)
    
    public static void test(Float...f)
    

    There are some rules that are followed when selecting which overloaded method to select, when Boxing, Widening, and Var-args are combined:

    1. Primitive widening uses the smallest method argument possible
    2. Wrapper type cannot be widened to another Wrapper type
    3. You can Box from int to Integer and widen to Object but no to Long
    4. Widening beats Boxing, Boxing beats Var-args.
    5. You can Box and then Widen (An int can become Object via Integer)
    6. You cannot Widen and then Box (An int cannot become Long)
    7. You cannot combine var-args, with either widening or boxing

    So, based on the above given rules:

    When you pass two integers to above functions,

    • according to rule 3, it will have to be first Widened and then
      Boxed to fit into a Long, which is illegal according to rule 5 (You cannot Widen and then Box).
    • So, it is Boxed to store in Integer var-args.

    But in first case, where you have methods with var-args of primitive types:

    public static void test(int...i)
    public static void test(float...f)
    

    Then test(1, 2) can invoke both the methods (Since neither of them is more suitable for rule 1 to apply):

    • In first case it will be var-args
    • In second case, it will be Widening and then Var-args (which is allowed)

    Now, when you have methods with exactly one int and one float:

    public static void test(int i)
    public static void test(float f)
    

    Then on invoking using test(1), rule 1 is followed, and smallest possible widening (i.e. the int where no widening is needed at all) is chosen. So 1st method will be invoked.

    For more information, you can refer to JLS - Method Invocation Conversion

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

Sidebar

Related Questions

I know why the following code doesn't compile: public class Main { public static
The following code doesn't compile in VS2012 class Zot { public: int A() {
why following code doesn't compile ? class aa1 <String> { public void fun(){ String
The following code doesn't work: class String{ public: char* str; int* counter; String(){ str
In C#, the following code doesn't compile: class Foo { public string Foo; }
Following code doesn't throw exception and prints success. Why ? #include <iostream> int main()
In the following code doesn't work as public void Foo() { CompanyDataContext db =
I don't understand why the following code doesn't work. (jQuery 1.6.4) HTML: <div class=test></div>
Take the following code: std::vector<std::vector<int>> v(10, 10); This code doesn't compile with libstdc++. It
The following code doesn't work (of course), because the marked line does not compile:

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.