简体   繁体   中英

Why doesn't the javac compiler flag this code with a warning about varargs ambiguity?

The following 2 lines of code compile OK. However, I think it's unuseable, because I can't think of any method invocations that will work with it.

// If I can't use this code, why not warn me about it?
void method(int... x) { }
void method(int x, int... y) { }

For instance, the following 2 lines of code both cause the javac compiler to issue an error message about varargs ambiguity.

// Both invocations fail to compile.
method(1);
method(1,1);  

I wondered if the javac compiler had its warnings messages disabled by default, and so I tried compiling my code with the javac -Xlint option, but that didn't change things. Any comments gratefully received. Thank you.

As DwB suggests in the comments

Try this: int[] blam = {1}; method(blam); method(1, blam); int[] blam = {1}; method(blam); method(1, blam);

In other words, you can still call the method you want by using values of type int[] where a vararg is declared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM