File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Overloading" Watch "Method Overloading" New topic
Author

Method Overloading

kashish verma
Greenhorn

Joined: Dec 07, 2005
Posts: 13
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?
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
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


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
kashish verma
Greenhorn

Joined: Dec 07, 2005
Posts: 13
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

Joined: Sep 28, 2004
Posts: 1228
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

Joined: Dec 07, 2005
Posts: 13
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?
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Overloading