Why overloading a varargs method doesn't work for the primitive type and its object wrapper type?
Example I does not compile. The error is "The method operation(String, Integer[]) is ambiguous for the type Program".
At compile time a vararg is converted to an array. In example I, 1 will be converted to an int array with 1 as the only element, something like int[] a={1}; with the autoboxing, 1 can also be converted to an Integer array, something like Integer [] b={1}; so if the Program class had only one operation() method, either method would work fine.
On the other hand, for method overloading to work, the compiler needs to figure out which method is the most-specific method. We already know that if the args are fixed, like in Example II:
Read full article from Why overloading a varargs method doesn't work for the primitive type and its object wrapper type?
No comments:
Post a Comment