SCJP Question: Ambiguous Method

Take a look at this code:

public class Test {
public static void main(String... args) {
    flipFlop("hello", new Integer(4), 2004);
    // flipFlop("hello", 10, 2004); // this works!
}

private static void flipFlop(String str, int i, Integer iRef) {
    System.out.println(str + " (String, int, Integer)");
}

private static void flipFlop(String str, int i, int j) {
    System.out.println(str + " (String, int, int)");
}

}

The compiler generates an error that causes ambiguity:

Description Resource path location type The flipFlop (String, int, Integer) method is ambiguous for the type Test Test.java scjp19 - inheritence / src line 3 Java problem

But if a commented line ti is used, it calls a flip flop, the method is uniquely called (the second, since autoboxing comes after using the primitive itself).

I would expect the compiler to see that the second argument will be unpacked anyway, and judge which method should be called depending on the third argument. Why is this not happening? What is the reason?

+3
source share
4 answers

flipFlop(String str, int i, int j) . - .

+6

flipFlop ( "", Integer (4), 2004); flipFlop (String str, int i, Integer iRef)

+1

Java 5 ( Integer int), , .

0

, , int/Integer @3rd. , @3rd , 2- autoUnboxed.

even this works: flipFlop ("hex", new Integer (4), new Integer (17));

while using the syntex and auto box functions it should call :: "private static void flipFlop (String str, int i, Integer iRef)", but all it says is AMBIGUOUS ..... ??

-1
source

Source: https://habr.com/ru/post/1735800/


All Articles