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


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

Re: Java Overloading rules confusion

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Java Overloading rules confusion
Date 2020-10-09 09:59 -0400
Organization Aioe.org NNTP Server
Message-ID <rlpqcl$gk8$1@gioia.aioe.org> (permalink)
References <rloivp$k46$1@dont-email.me>

Show all headers | View raw


On 10/8/2020 10:47 PM, Wayne wrote:
> I have a question on overloading in Java.  I find the language
> specifications confusing on this issue.
> 
> 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
>          m(3, 4);
>          ^
>    both method m(double,int) in Foo and method m(int,float) in Foo match
> 
> Shouldn't "m(int,float)" be more specific than "m(double,int)"?
> 
> (This works IF you change the first m(double,int) to m(int,double).)

There are some explanation in JLS (other have already elaborated
on that).

But I would like to point out the obvious: the first code
may compile, but it is still bad code.

Code where the reader needs to remember some subtle
language rules to understand why the code does what
it does is bad code.

Arne



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