Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when we pass int arguments to the overloading method having float as a parameter for one method and another having double param

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

like image 829
Hariprasath Avatar asked Jun 18 '14 07:06

Hariprasath


People also ask

What will happen if overloaded functions have same arguments but different return type?

Overloading with same arguments and different return type − The return type doesn't matter. If they don't have different parameters, they both are still considered as same method and a compile time error will be generated.

What happens when an overloaded method is invoked?

Overloaded methods are differentiated based on the number and type of parameter passed as arguments to the methods. If we try to define more than one method with the same name and the same number of arguments then the compiler will throw an error.

Can a method be overloaded based on different return type but same argument type?

Method overloading cannot be done by changing the return type of methods. The most important rule of method overloading is that two overloaded methods must have different parameters.

Why method overloading is not possible by changing the return type of method only?

Q) Why Method Overloading is not possible by changing the return type of method only? In java, method overloading is not possible by changing the return type of the method only because of ambiguity.


1 Answers

Testing a variant of your code, except with a byte literal and overloaded methods with various combinations of short, int, and long appears to imply that the compiler chooses the "least widening" conversion if more than one is available.

Thus:

  • Between a short and an int, if you call the overloaded method with a byte, the short variant will be chosen
  • Between an int and a long, if you call the overloaded method with a byte or short, the int variant will be chosen

And so forth.

Thus, because long can be widened to either float or double, and because the float conversion is the "least widening", the float 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:

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.

So by this, a method that takes a float is "more specific" than a method that takes a double because any invocation handled by a method that takes a float can always be handled by a method that takes a double, but not the other way around.

like image 91
awksp Avatar answered Oct 21 '22 06:10

awksp