0

I have the following methods:

static void f(double x)
{
    System.out.println("f(double)");
}

static void f(Double xObj)
{
    System.out.println("f(Double)");
}

static void f(double... s)
{
    System.out.println("f(double...)");
}

public static void main(String[] args)
{
    double x1 = 8.5;
    Double xO1 = 5.25;
    f(x1);
    f(xO1);
}

Output:

f(double)

f(Double)

The rule of searching an overloaded method is the following:

  1. Search for the overloaded method without including method with auto (un)boxing and method with ellipsis.
  2. If no method is found, search again with including method with auto (un)boxing.
  3. If no method is found, search again with including method with ellipsis.

This rule is applicable when calling method f with primitive parameter, but when calling with auto boxing parameter this rule is not applicable.

Can anyone explain me if this rule is correct or not ? and what is the correct one ?

Thanks for advance :)

Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
  • 1
    I don't see where you're confused. What did you think would get printed out? – Sotirios Delimanolis Mar 27 '14 at 00:42
  • If the rule is applicable then when calling method `f` with an auto (un)boxing parameter it should call method `void f(double)` – Naruto Biju Mode Mar 27 '14 at 00:44
  • 1
    Don't think of it as `auto (un)boxing parameter`. It's primitive types and reference types. The first check checks for an exact match with those. So an argument of type `Double` uses the overloaded method with a `Double` parameter. – Sotirios Delimanolis Mar 27 '14 at 00:46
  • The crucial point here is that each overloaded method is unique, so there's no preferential treatment given - no "search order" to figure out rules for. f(Double) is a fundamentally different method from f(double). Java doesn't have to figure out which one to use because you have to either call f(double) or f(Double) explicitly. – Brian Bauman Mar 27 '14 at 00:49
  • possible duplicate of [Why does autoboxing make some calls ambiguous in Java?](http://stackoverflow.com/questions/501412/why-does-autoboxing-make-some-calls-ambiguous-in-java) – Sotirios Delimanolis Mar 27 '14 at 00:59
  • But if i comment `f(Double)` and call `f(xO1)`, it will calls the method `f(double)`. And if comment `f(double)` and call `f(x1)`, it will calls the method `f(Double)` – Naruto Biju Mode Mar 27 '14 at 01:01
  • @T-Rex But if i comment `f(Double)` and call `f(xO1)`, it will calls the method `f(double)`. And if comment `f(double)` and call `f(x1)`, it will calls the method `f(Double)` – Naruto Biju Mode Mar 27 '14 at 01:07

1 Answers1

1

Java picks the most specific method that fits the arguments using this rule

"This rule is applicable when calling method f with primitive parameter, but when calling with auto boxing parameter this rule is not applicable."

When you remove the method f(Double), f(xO1) will call f(double) because that was the only, and the most specific method that fits the arguments.

For reference you may check the Java Language Specification

Baby
  • 5,062
  • 3
  • 30
  • 52
  • i think the order is correct can you see the part [15.12.2](Java Language Specification) – Naruto Biju Mode Mar 27 '14 at 01:40
  • Oh yes! how can i missed that. so the answer of your question should be the question itself. Java picks the most specific method that fits the arguments using [this rule](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2) – Baby Mar 27 '14 at 01:57