In overloading concept, i am having one doubt, that is . when i comes to overload the method with int value the method call's the float parameter method rather the double parameter method.
void method1(float f){
System.out.println('float');
}
void method1(double f){
System.out.println('double');
}
method call:
method1(10);
output: float
As stated in the java tutorials in this link A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.
For the above case the method call should call the double parameter method. But it call's float parameter method.
How the process of overloading taking place in this area?.
as per http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
Testing a variant of your code, except with a
byte
literal and overloaded methods with various combinations ofshort
,int
, andlong
appears to imply that the compiler chooses the "least widening" conversion if more than one is available.Thus:
short
and anint
, if you call the overloaded method with abyte
, theshort
variant will be chosenint
and along
, if you call the overloaded method with abyte
orshort
, theint
variant will be chosenAnd so forth.
Thus, because
long
can be widened to eitherfloat
ordouble
, and because thefloat
conversion is the "least widening", thefloat
overload is chosen.I think this is because of the "choose the most specific overload" way that the compiler resolves multiple possible overloads. From the JLS, section 15.12.2.5:
So by this, a method that takes a
float
is "more specific" than a method that takes adouble
because any invocation handled by a method that takes afloat
can always be handled by a method that takes adouble
, but not the other way around.In Java,there is relation between sub class and super class and also ascending level for primitives from byte short.... to double.
The rule is, whenever there is ambiguity which overloaded method to choose, the most near one sub class overloaded method or nearest primitive in ascending order is chosen.