Sunday, October 5, 2025
HomeLanguagesJavaMethod Overloading and Ambiguity in Varargs in Java

Method Overloading and Ambiguity in Varargs in Java

Prerequisite – Varargs , Method Overloading

Method Overloading in Varargs

Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. We can overload a method that takes a variable-length argument by following ways:

  • Case 1 – Methods with only Varargs parameters: In this case, Java uses the type difference to determine which overloaded method to call. If one method signature is strictly more specific than the other, then Java chooses it without an error.




    //Java program to illustrateΒ 
    //method overloading in varargs
    public class varargsDemo
    {
    Β Β Β Β public static void main(String[] args)
    Β Β Β Β {
    Β Β Β Β Β Β Β Β fun();
    Β Β Β Β }
    Β Β 
    Β Β Β Β //varargs method with float datatype
    Β Β Β Β static void fun(float... x)
    Β Β Β Β {
    Β Β Β Β Β Β Β Β System.out.println("float varargs");
    Β Β Β Β }
    Β Β Β Β Β Β 
    Β Β Β Β //varargs method with int datatype
    Β Β Β Β static void fun(int... x)
    Β Β Β Β {
    Β Β Β Β Β Β Β Β System.out.println("int varargs");
    Β Β Β Β }
    Β Β Β Β Β Β 
    Β Β Β Β //varargs method with double datatype
    Β Β Β Β static void fun(double... x)
    Β Β Β Β {
    Β Β Β Β Β Β Β Β System.out.println("double varargs");
    Β Β Β Β }
    }

    
    

    Output:

    int varargs
    

    This output is due to the fact that int is more specific than double. As specified in the JLS section 15.12.2.5, 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 according to type promotion. The following rules define the direct supertype relation among the primitive types in this case:

    • double > float
    • float > long
    • long > int
    • int > char
    • int > short
    • short > byte
  • Case 2 – Methods with Varargs alongwith other parameters In this case, Java uses both the number of arguments and the type of the arguments to determine which method to call.

    Below is the java program that overloads fun( ) method three times:




    // Java program to demonstrate VarargsΒ 
    // and overloading.
    class TestΒ 
    {
    Β Β Β Β // A method that takes varargs(here integers).
    Β Β Β Β static void fun(int ... a)Β 
    Β Β Β Β {
    Β Β Β Β Β Β Β Β System.out.print("fun(int ...): " +
    Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β "Number of args: " + a.length +
    Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β " Contents: ");
    Β Β Β Β Β Β Β Β Β Β 
    Β Β Β Β Β Β Β Β // using for each loop to display contents of a
    Β Β Β Β Β Β Β Β for(int x : a)
    Β Β Β Β Β Β Β Β Β Β Β Β System.out.print(x + " ");
    Β Β Β Β Β Β Β Β Β Β 
    Β Β Β Β Β Β Β Β System.out.println();
    Β Β Β Β }
    Β Β Β Β Β Β 
    Β Β Β Β // A method that takes varargs(here booleans).
    Β Β Β Β static void fun(boolean ... a)
    Β Β Β Β {
    Β Β Β Β Β Β Β Β System.out.print("fun(boolean ...) " +
    Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β "Number of args: " + a.length +
    Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β " Contents: ");
    Β Β Β Β Β Β Β Β Β Β 
    Β Β Β Β Β Β Β Β // using for each loop to display contents of a
    Β Β Β Β Β Β Β Β for(boolean x : a)
    Β Β Β Β Β Β Β Β Β Β Β Β System.out.print(x + " ");
    Β Β Β Β Β Β Β Β Β Β 
    Β Β Β Β Β Β Β Β System.out.println();
    Β Β Β Β }
    Β Β Β Β Β Β 
    Β Β Β Β // A method takes string as a argument followed by varargs(here integers).
    Β Β Β Β static void fun(String msg, int ... a)Β 
    Β Β Β Β {
    Β Β Β Β Β Β Β Β System.out.print("fun(String, int ...): " +
    Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β msg + a.length +
    Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β " Contents: ");
    Β Β Β Β Β Β Β Β Β Β 
    Β Β Β Β Β Β Β Β // using for each loop to display contents of a
    Β Β Β Β Β Β Β Β for(int x : a)
    Β Β Β Β Β Β Β Β Β Β Β Β System.out.print(x + " ");
    Β Β Β Β Β Β Β Β Β Β 
    Β Β Β Β Β Β Β Β System.out.println();
    Β Β Β Β }
    Β Β Β Β Β Β 
    Β Β Β Β public static void main(String args[])
    Β Β Β Β {
    Β Β Β Β Β Β Β Β // Calling overloaded fun() with differentΒ  parameter
    Β Β Β Β Β Β Β Β fun(1, 2, 3);
    Β Β Β Β Β Β Β Β fun("Testing: ", 10, 20);
    Β Β Β Β Β Β Β Β fun(true, false, false);
    Β Β Β Β }
    }

    
    

    Output:

    fun(int ...): Number of args: 3 Contents: 1 2 3 
    fun(String, int ...): Testing: 2 Contents: 10 20 
    fun(boolean ...) Number of args: 3 Contents: true false false 
    

Varargs and Ambiguity

Sometimes unexpected errors can result when overloading a method that takes a variable length argument. These errors involve ambiguity because both the methods are valid candidates for invocation. The compiler cannot decide onto which method to bind the method call.




// Java program to illustrate Varargs and ambiguity
class TestΒ 
{
Β Β Β Β // A method that takes varargs(here integers).
Β Β Β Β static void fun(int ... a)Β 
Β Β Β Β {
Β Β Β Β Β Β Β Β System.out.print("fun(int ...): " +
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β "Number of args: " + a.length +
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β " Contents: ");
Β Β Β Β Β Β Β Β Β Β 
Β Β Β Β Β Β Β Β // using for each loop to display contents of a
Β Β Β Β Β Β Β Β for(int x : a)
Β Β Β Β Β Β Β Β Β Β Β Β System.out.print(x + " ");
Β Β Β Β Β Β Β Β Β Β 
Β Β Β Β Β Β Β Β System.out.println();
Β Β Β Β }
Β Β Β Β Β Β 
Β Β Β Β // A method that takes varargs(here booleans).
Β Β Β Β static void fun(boolean ... a)
Β Β Β Β {
Β Β Β Β Β Β Β Β System.out.print("fun(boolean ...) " +
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β "Number of args: " + a.length +
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β " Contents: ");
Β Β Β Β Β Β Β Β Β Β 
Β Β Β Β Β Β Β Β // using for each loop to display contents of a
Β Β Β Β Β Β Β Β for(boolean x : a)
Β Β Β Β Β Β Β Β Β Β Β Β System.out.print(x + " ");
Β Β Β Β Β Β Β Β Β Β 
Β Β Β Β Β Β Β Β System.out.println();
Β Β Β Β }
Β Β Β Β Β Β 
Β Β Β Β public static void main(String args[])
Β Β Β Β {
Β Β Β Β Β Β Β Β // Calling overloaded fun() with differentΒ  parameter
Β Β Β Β Β Β Β Β fun(1, 2, 3); //OK
Β Β Β Β Β Β Β Β fun(true, false, false); //OK
Β Β Β Β Β Β Β Β fun(); // Error: Ambiguous!
Β Β Β Β }
}


In above program, the overloading of fun( ) is perfectly correct. However, this program will not compile because of the following call:

fun(); // Error: Ambiguous!

According to (JLS 15.2.2), there are 3 phases used in overload resolution: First phase performs overload resolution without permitting boxing or unboxing conversion, Second phase performs overload resolution while allowing boxing and unboxing and Third phase allows overloading to be combined with variable arity methods, boxing, and unboxing. If no applicable method is found during these phases, then ambiguity occurs.
The call above could be translated into a call to fun(int …) or fun(boolean …). Both are equally valid and do not be resolved after all three phases of overload resolution because both the data types are different. Thus, the call is inherently ambiguous.

Another example of ambiguity:
The following overloaded versions of fun( )are inherently ambiguous:

static void fun(int ... a) { // method body  }
static void fun(int n, int ... a) { //method body }

Here, although the parameter lists of fun( ) differ, there is no way for the compiler to resolve the following call:

fun(1)

This call may resolve to fun(int … a) or fun(int n, int … a) method, thus creating ambiguity. To solve these ambiguity errors like above, we will need to forego overloading and simply use two different method names.

This article is contributed by Gaurav Miglani. If you like Lazyroar and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

RELATED ARTICLES

Most Popular

Dominic
32337 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6706 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11935 POSTS0 COMMENTS
Shaida Kate Naidoo
6823 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6779 POSTS0 COMMENTS