• Variable length(vararg) and Ambiguity in vararg

    variable length 

    The feature that allows creation of a method that need to take variable number of arguments is called varargs and it is short for variable length arguments .A method that can take variable number of arguments is called a variable ____ method as simply vararg method . In case where the maximum number of potential arguments is larger and unknowable , an approach is used in which the arguments were put into an array and then the array was passed to the method.


    The(…) syntax also known as ellipsis simply tells the compiler that a variable number of arguments will be used and that these arguments will be stored in an array referred by(a). Main method can show method with different number of arguments including no arguments at all . the arguments automatically put in an array and passed to (a).In case of no argument length of array is zero.

    Ambiguity in vararg

    There are two cases where it is possible to create an ambiguous call to an overloaded vararg method 
    CASE 1 :

    In the above code the overloading of show method is perfectly correct however the program will not compile because if the var args parameters is empty this call could be translated into a call to both show(int…) and show(Boolean …) thus the call is inherently ambiguous
    CASE 2 :

    Although show deference in parameter list. There is no way compiler can resolve the call it is impossible for compiler to translate the call because and it is unable to decide to whether call show (int…) with no var args argument since both the calls are valid . Thus this situation is also ambiguous.
  • You might also like

    No comments:

    Post a Comment

search topics

NEWSLETTER

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!