It's just an exercise but I can't figure out the ambiguity:
private static void flipFlop(String str, int i, Integer iRef) {
System.out.println(str + "ciao");
}
private static void flipFlop(String str, int i, int j) {
System.out.println(str + "hello");
}
public static void main(String[] args) {
flipFlop("hello", new Integer(4), 2004);
}
It says:
The method flipFlop(String, int, Integer) is ambiguous for the type Test
I would have guessed that the second argument would have been unwrapped to int and so the second flipFlop
method would have been the choice.
Q. Which two primitives have wrapper classes that are not merely the name of the primitive with an uppercase letter? Explanation: The wrapper class for int is Integer and the wrapper class for char is Character. All other primitives have the same name.
All primitive wrapper classes (Integer, Byte, Long, Float, Double, Character, Boolean and Short) are immutable in Java, so operations like addition and subtraction create a new object and not modify the old.
If you enjoy riveting reading, here is the relevant portion of the Java Language specification that describes how methods are resolved.
But basically your third argument can be interpreted as a primitive or an autoboxed wrapper, and the compiler can't figure out what you want. Both methods are "maximally specific" to use the JLS terminology.
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