• 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

primitive method calls

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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};
 
Mamta Sharma
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mike, I got it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic