2

Note: this question has been marked as a duplicate of Ambiguous varargs methods but it is not a duplicate. That question deals with confusion between Integer/int (here, it would be Double/double). But my question deals with ambiguity between double/lack of double, and puzzles about the lack of ambiguity when using Double instead.

Why does Java disambiguate based on Double but not double in the cases below?

public class Ambiguity {

    void foo(double d, Object... o) {}

    void foo(Object... o) {}

    void bar() {
        foo(1.0d, "Hi"); // complains about ambiguity
    }

    void foo2(Double d, Object... o) {}

    void foo2(Object... o) {}

    void bar2() {
        foo2(1.0d, "Hi"); // fine
    }
}
user118967
  • 4,895
  • 5
  • 33
  • 54
  • I can,t find the answer of this question in the associated thread... – dbl Aug 18 '18 at 07:47
  • After reafing all the answers, without any further research nor tests the only clue I could think of is that since Double is subtype of Object the second example compiles.... – dbl Aug 18 '18 at 07:58
  • Beacause Double is an Object, whereas double is not. – JohanLarsson Aug 18 '18 at 08:12
  • But that makes it sound like the version with Double would be the ambiguous one... but the opposite happens. – user118967 Aug 18 '18 at 08:25
  • The answer you are looking for is indeed already been given to the question this is supposedly the duplicate of: https://stackoverflow.com/a/27880923 – Giulio Piancastelli Aug 18 '18 at 09:40

0 Answers0