3

I know about overloading rules in Java, but for some situations my intuition doesn't work.

Let's consider an example:

public class Overloading {
    public static void main(String[] args) {
        long primitive = 3;
        Long boxed = Long.valueOf(5);
        doWork(primitive, boxed);     //1
        doWork(boxed, boxed);         //2
        doWork(primitive, primitive); //3                
    }

    static void doWork(Long a, Long b) {}

    static void doWork(long a, Long b) {} 
}

Do you know what (1, 2 or 3) will be compiled successfully?

The first and the second will, but third won't (due to ambitious method call).

Why is javac designed this way and can't resolve this situation? Why not to cast #3 to #1?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Dmitry P.
  • 824
  • 5
  • 14
  • 3
    And which one of the implementations should Java choose for you ? That ambiguity is exactly the reason that the implementers chose to return an error. – Nir Alfasi Sep 12 '13 at 15:17
  • 5
    See Related: http://stackoverflow.com/questions/501412/why-does-autoboxing-make-some-calls-ambiguous-in-java?rq=1 – Sotirios Delimanolis Sep 12 '13 at 15:18
  • 2
    @alfasin I expected that java can choose an option with minimum possible autoboxing operations. I was wrong, the phrase: "Also: the compiler can't always choose the most specific method based on the number of auto(un)boxing needed." from the topic poited by Sotirios explains it. Thanks! – Dmitry P. Sep 12 '13 at 15:26

2 Answers2

2

Good Question!

You're suggestion that #3 should be cast to #1 does seem to make sense, because it has one less auto-boxing to do.

This is probably the reasoning behind Java's decision to give you an error instead of picking the method with the least boxing involved:

  1. This situation is rare.
  2. Figuring out which method to use for auto-boxing could be expensive*.
  3. The logic behind which method to choose could be too subjective.
  4. Therefore, in this case, Java forces you to not be ambiguous.

*The accepted answer to the question linked to by Sotirios gives some insight into why it might by too expensive to be worth it.

Community
  • 1
  • 1
James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • It does not have anything to do with the algo being expensive - it is mostly for backward compatibility reasons. – assylias Sep 12 '13 at 16:20
  • @assylias Good point about backward compatibility; for this reason Java would not want to change the rules in future versions. But it still begs the question about the original decision in Java 1.5 when this became an issue. – James Dunn Sep 12 '13 at 17:21
0

The first and the second will, but third won't (due to ambidous method call). Why java can't resolve this situation? Why not to cast #3 to #1?

As you mentioned, long can be autoboxed to Long. However, there is an ambiguity here - if only the second long is autoboxed, you'd get #1. If both of them are autoboxed, you'd get #2.

Since Java cannot decide which method you meant, it raises an error.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Theoretically, Java could have been designed to choose one of the methods over the other -- I think Dmitri's question was why it was not designed this way. – James Dunn Sep 12 '13 at 15:23