why does it select the function with float instead of double?????
public class Test
void show(double a)
void show(float f)
public static void main(String args)
Test t=new Test();
t.show(4); //4.0 is an int value
Because the most specific method is chosen. If that link and the related discussion of method invocation conversion and widening primitive conversion is difficult to follow, avoid overloading.
Basically it boils down to the fact that a float may be converted to a double by means of a widening primitive conversion, but not vice versa. A method wit a float argument is said to be more specific because of that.
You can tell Java to use the double parametered method
my casting to double, this is normally only used when the
compiler generates an error due to ambiguousy when using null.
Or if you used null
But otherwise, if you do not cast, pbrockway2 had the answer.
This is known as tricky overloading
overloading is a feature found in different programming languages such as Ada, C # in, VB.Net, C + + in, D, and Java, allows you to create a variety of methods with the same name, each with the other provisions of Types of input and output types. I am intrested in Java programming.
If you want a number to be a "float" then you have to put an "f" at the end
now void show(float); function will be called.
Originally Posted by Bhuvan
the comment is wrong. in java decimal values are interpreted as a double by default.