• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method Overloading

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q)class GFC215 {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
int a1 = 1; long b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}

What is the result of attempting to compile and run the program?

a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compile-time error
f. Run-time error
g. None of the above
----------------------
Output is float,float.
But why can't it be double, double?
Can anyone explain this?
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because float is most nearer to int than a double. In the other way precision of a double variable is greater than a float hence int is promoted to a float rather than a double variable
 
kashish verma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But then what about long? And one more question like the earlier one.
I am really confused.Actually on what basis ,we r selecting the method m()?

class GFC217 {
static String m(int i) {return "int";}
static String m(float i) {return "float";}
public static void main (String[] args) {
long a1 = 1; double b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}

What is the result of attempting to compile and run the program?

a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compile-time error
f. Run-time error
g. None of the above
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should get a compile time error because , you are passing a double to the method m() but there are no methods that receives a double parameter.A float can be automatically promoted to a double , but a double cannot be automatically coverted to a float , explicit type cast is needed.
 
kashish verma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the answer given:
The method invocation expression, m(b1), contains an argument of type double. A method invocation conversion will not implicitly narrow the argument to match the parameter type of the method, m(float i). The method invocation expression, m(a1), contains an argument of type long. A method invocation conversion will widen the argument to match the parameter type of the the method, m(float i).
srrini,
What i am not able to understand is that double to float can't be done implicitly(i agree) but why then long to float is allowed?
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All, after starting at the screen, I had a "Eureka!" moment. I thought that as we're using an int literal 2 (and not explicitly 2l), I thought this was getting resolved at compile time. I applied a variation by creating a new long var, c, and assigning to it the value of b1 and calling the m method with c but that didn't work. i.e


Then I applied a long literal and tried it again but that didn't work as well.

I'm as flummoxed as all of you.
Well, I"m a little less flummoxed now: JLS Conversions states that a long to float or double conversion is a widening conversion.
Hm, I understand what that means but not why it means what it means.
Given the above, it's possible that a long is implicitly promoted to a float rather than a double.
Check this out:

Take a guess: do you think the above would compile? Run without exceptions?
It does both! No "loss of precision" compiler complaint! JVM happily ran it and printed out:
x is 2 and y is 2.0
Hence, ladies and gentlemen, long to float is considered "widening" and accepted by the compiler AND the JVM. The default conversion would promote a long to a float rather than a double. If you want double, ask for it! Do it explicitly.
Further digging necessary?

[ December 15, 2005: Message edited by: Sasikanth Malladi ]
[ December 15, 2005: Message edited by: Sasikanth Malladi ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic