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
double. (This is curious anyways because the first can cause loss ofprecision while the latter does not.)
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).)
I have a question on overloading in Java. [...]
Shouldn't "m(int,float)" be more specific than "m(double,int)"?
...
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
On 10/8/2020 10:47 PM, Wayne wrote:
I have a question on overloading in Java. [...]
Shouldn't "m(int,float)" be more specific than "m(double,int)"?
Had to spend some time studying JLS 15.12.2.5 to find that the
answer is "No." There's a lot if niggly detail in the JLS, but for
me the easiest explanation comes in the introductory section:
"The informal intuition is that one method is more specific
than another if any invocation handled by the first method
could be passed on to the other one without a compile-time
error."
Let's try this out with your two versions of m(). Clearly, both
versions could (in isolation) handle m(3,4) or m('3',4) and many
other invocations. However, m(3,4.0f) would work only with the
(int,float) version, but only (double,int) could handle m(3.0,4).
That is, for each version there is an invocation that it could
accept but would not work with the other. So (says "informal
intuition") neither is "more specific" than the other, because
each has valid invocations the other could not accept.
Interesting query. Thanks!
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (0 / 16) |
Uptime: | 167:40:15 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,540 |