This code:
public static void f(String[] args) {}
public static void f(Integer[] args) {}
public static void main(String[] args)
{
f(Stream.of("xxx").toArray(i -> new String[i]));
}
compiles success with jdk8u45 but jdk8u60 prints the following error:
Error:(17, 9) java: reference to f is ambiguous
both method f(java.lang.String[]) in type_infer.Test and method f(java.lang.Integer[]) in type_infer.Test match
Does it follow the JSL, Why compiler can not resolve overloaded methods? Was it a fixed bug in jdk8u45?
This looks like a bug recently introduced in javac
.
The argument in the call to f(..)
is clearly of type String[]
.
This can, e.g., be shown by resolving the same expression as a standalone expression:
String s1 = Stream.of("xxx").toArray(i -> new String[i])[0];
String[] s2 = Stream.of("xxx").toArray(i -> new String[i]).clone();
These are accepted by all versions of compilers.
Clearly, f(Integer[])
is not applicable with an argument of type String[]
. I see no reason why the outer type inference would force the inner resolution to a worse result (e.g., Object[]
) than resolution as a standalone expression.
Additional evidence: the following statement is correctly rejected even by those versions of javac
affected by this bug:
Integer[] s3 = Stream.of("xxx").toArray(i -> new String[i]);
Hence f(Integer[])
is clearly out of the reckoning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With