Consider the following example code:
public class TestClass {
public void doSth(String str, String l, Object... objects) {
System.out.println("A");
}
public void doSth(String str, Object... objects) {
System.out.println("B");
}
}
When I now call new TestClass().doSth("foo", "bar")
I get the expected result A
. But if I change the method signature of the first method by chaging the parameter l
to a primitive type:
public class TestClass {
public void doSth(String str, long l, Object... objects) {
System.out.println("A");
}
public void doSth(String str, Object... objects) {
System.out.println("B");
}
}
calling new TestClass().doSth("foo", 2L)
will yield a reference to call ambiguous
compile time error.
I thought about that one for some time now and also consulted this stackoverflow question, but I was unable to understand why this happens. In my opinion the doSth("foo", 2L)
is more specific to the doSth(String string, long l, Object... obj)
signature and should allow the compiler to also come to this conclusion.
long
toLong
in the function declaration solved it for me. – Ivers