Author Topic:   Overloading confusion
Veerpal
unregistered
posted April 22, 2000 01:58 AM           
Hi all

I am confused on which method will be called when two overloaded method will have primitives parameters

eg.\

meth( float f double d , long l)
meth( double , float f , int i)

called with meth( 7 , 7.0 , 7l)

when compiler error comes with ambigous call to method
and if not then how to decide which will be called

Thanks
VS

Tony Alicea
sheriff
posted April 22, 2000 08:14 AM             
I don't see why there should be an ambiguity there. I am not at home and as such I don't have a Java compiler here, but it looks like, because the second arg of the calling method is a double, the second:

meth( double , float f , int i)

would not compile. This one

meth( float f, double d , long l) would compile since it is called this way:

called with meth( 7 , 7.0 , 7l)

An int can be converted automatically to float and double, but not a double to float.

maha anna
bartender
posted April 23, 2000 06:10 PM             
Veerpal,
For your given method definitions and the mathod invocation type there would not be any problem. Because out of those 2 method definitions when you call with meth( 7 , 7.0 , 7l);
one method definition is more specific than the other .

And also you got a valid doubt of when will the ambiguity come?

Try to call the same methods (as given by you), with the foll.

meth(7,7,7);

Now, the ambiguity WILL arise. Compiler is confused. It does not know which method is more suitable with arg as (int,int,int). Since both are equially suitable, there comes the confusion.

regds
maha anna

Eric Barnhill
ranch hand
posted April 25, 2000 10:17 PM         
Tony,

won't the second also not compile becase the long won't be automatically be converted to an int?

Thanks
Eric

Harry Chawla
unregistered
posted April 26, 2000 10:31 PM           
Hi though I'm relatively new to Java, still it's apparent that 1st method will be called because a floating point literal with no F or D suffix defaults to a 64-bit literal and secondly
the third parameter has l(L) suffixed to it, denoting long datatype.
Pls correct me, if I'm wrong.

nirvan sage
greenhorn
posted April 27, 2000 04:22 AM             
yes It does compile.Try what has maha has pointed out.One more thing the compiler points out an error at the method call not at the method declaration.

Java Nut
unregistered
posted April 27, 2000 05:28 AM           
Eric,
A long won't automatically be converted to an int because it is a narrowing conversion which needs an explicit cast. On the other hand, an int can be automatically be converted to a long because it is a widening conversion.

|