Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39616

Re: Java Overloading rules confusion

From Marcel Mueller <news.5.maazl@spamgourmet.org>
Newsgroups comp.lang.java.programmer
Subject Re: Java Overloading rules confusion
Date 2020-10-09 09:01 +0200
Organization MB-NET.NET for Open-News-Network e.V.
Message-ID <rlp1sc$61a$1@gwaiyur.mb-net.net> (permalink)
References <rloivp$k46$1@dont-email.me>

Show all headers | View raw


Am 09.10.20 um 04:47 schrieb Wayne:
> Why will this:
> 
> class Foo {
>      public static void main ( String[] args ) {
>          m(3, 4);
>      }
>      static void m (double d1, int i2) {
>          System.out.println( "m(d,i)" );
>      }
>      static void m (float f1, int i2) {
>          System.out.println( "m(f,i)" );
>      }
> }
> 
> Compile and run, producing "m(f,i)", but this:
> class Foo {
>      public static void main ( String[] args ) {
>          m(3, 4);
>      }
>      static void m (double d1, int i2) {
>          System.out.println( "m(d,i)" );
>      }
>      static void m (int i1, float f2) {
>          System.out.println( "m(i,f)" );
>      }
> }
> 
> does not compile and gives this error:
> Foo.java:3: error: reference to m is ambiguous

Very interesting!

 From my point of view this is a bug. But probably it is conformant to 
the rules.

Different widening conversions (float vs. double) do not have different 
priorities when searching for the most specific method during overload 
resolution. So both methods in example 2 have one parameter that 
requires a widening conversion.

But on the other side, widening conversions follow the sequence byte, 
short, int, long, float, double. So int -> float is preferred over int 
-> double. (This is curious anyways because the first can cause loss of 
precision while the latter does not.)
So the call is /not/ ambiguous when the different widening conversions 
apply to the /same/ parameter. Then the priority applies.

Not intuitive, not very helpful, but this seems to be the rules.


Marcel

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Java Overloading rules confusion Wayne <wayne@nospam.invalid> - 2020-10-08 22:47 -0400
  Re: Java Overloading rules confusion Marcel Mueller <news.5.maazl@spamgourmet.org> - 2020-10-09 09:01 +0200
  Re: Java Overloading rules confusion Eric Sosman <esosman@comcast-dot-net.invalid> - 2020-10-09 09:21 -0400
    Re: Java Overloading rules confusion Wayne <wayne@nospam.invalid> - 2020-10-09 22:51 -0400
  Re: Java Overloading rules confusion Arne Vajhøj <arne@vajhoej.dk> - 2020-10-09 09:59 -0400
    Re: Java Overloading rules confusion Wayne <wayne@nospam.invalid> - 2020-10-09 22:51 -0400

csiph-web