-4

If I want to have a method

void mymethod(float val) {}

and another one

void mymethod (int val) {}

then when I call mymethod (1.2) will it correctly detect the first one is the right one to call?

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 2
    have you tried this? – namingFailed Dec 01 '16 at 08:57
  • 2
    Possible duplicate of [What happens when we pass int arguments to the overloading method having float as a parameter for one method and another having double param](http://stackoverflow.com/questions/24279680/what-happens-when-we-pass-int-arguments-to-the-overloading-method-having-float-a) – Zia Dec 01 '16 at 08:58
  • Read about [overriding vs overloading](http://stackoverflow.com/questions/12374399/what-is-the-difference-between-method-overloading-and-overriding) – PeterMmm Dec 01 '16 at 08:59
  • http://beginnersbook.com/2013/05/method-overloading/ seems to says it can looking at case 4 – Andrew Barnes Dec 01 '16 at 09:09

1 Answers1

0

mymethod(1.2) will look for a mymethod(double d) method since literal 1.2 for an inlined numeric value is stored in a double by default.

You should write : mymethod(1.2F)

In this case, overloading will work and mymethod(float val) will be called.

Overloading allows to name multiple methods with the same name but with distinct parameters. At compile time, the compiler chooses the method which matches with effective parameters.

In your case, mymethod() method is overloaded since :

 void mymethod(float val) {}
 void mymethod(int val) {}
davidxxx
  • 125,838
  • 23
  • 214
  • 215