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


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

Java Overloading rules confusion

From Wayne <wayne@nospam.invalid>
Newsgroups comp.lang.java.programmer
Subject Java Overloading rules confusion
Date 2020-10-08 22:47 -0400
Organization A noiseless patient Spider
Message-ID <rloivp$k46$1@dont-email.me> (permalink)

Show all headers | View raw


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).)

-- 
Wayne

Back to comp.lang.java.programmer | Previous | Next — 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