Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do two methods with signature (primitive, wrapper) and (primitive, primitive) cause the method call (wrapper, primitive) to be ambiguous?

Tags:

java

jdk1.6

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.

like image 930
dierre Avatar asked Jan 04 '14 16:01

dierre


People also ask

Which two primitives have wrapper classes?

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.

Are the wrappers for primitive types mutable or immutable?

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.


1 Answers

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.

like image 75
Vidya Avatar answered Oct 06 '22 00:10

Vidya