| Author |
primitive method calls
|
Mamta Sharma
Greenhorn
Joined: Jun 03, 2008
Posts: 25
|
|
Question is from: John Meyers's SCJP 5 Mock Exam: class test { public static void main ( String [] args ) { methodOne(20); } static void methodOne( long l ) { System.out.println("long"); } static void methodOne( float f ) { System.out.println("float"); } } Answer is : long I agree with the answer but the explanation :"Prints long. It prints long because it is the most specific choice. Likewise between two methods that accept float or double, float will be chosen.": says between a float & double, float will be chosen.I have tried this out with 20.0 but it prints out double. Here is what i did: public class PrimitiveMethodCalls { public static void main ( String [] args ) { methodOne(20.0); } static void methodOne( float l ) { System.out.println("float"); } static void methodOne( double f ) { System.out.println("double"); } } Please Explain. Thanks Mamta Sharma [ August 01, 2008: Message edited by: Mamta Sharma ]
|
 |
Mike Mitchell
Ranch Hand
Joined: May 28, 2008
Posts: 37
|
|
20.0 in your example is a double literal... (which is why something like the following won't compile...) Float f = new Float[] {20.0, 21.0};
|
SCJP 5, SCWCD 5
|
 |
Mamta Sharma
Greenhorn
Joined: Jun 03, 2008
Posts: 25
|
|
|
Thanks Mike, I got it.
|
 |
 |
|
|
subject: primitive method calls
|
|
|