Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't ambiguity error?

Tags:

java

public static void main(String[] a){
          VarArgs obj = new VarArgs();
          obj.add(1,2);
}

class VarArgs{
       int add(int size, Integer... params){
       }
}

This code works. But I thought it will give ambiguity error. Because of type casting.

How does it work?

like image 240
Gibbs Avatar asked Mar 31 '26 20:03

Gibbs


1 Answers

The reference to call ambiguous error happens when there are two methods that are equally applicable; and in fact, one common way to fix that error is to remove one of the two methods. (See Compiler error : reference to call ambiguous.)

In your case, there's only one method to begin with, so there's no ambiguity: the 2 simply gets autoboxed into Integer.valueOf(2), then into new Integer[] { Integer.valueOf(2) }.

like image 165
ruakh Avatar answered Apr 03 '26 09:04

ruakh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!