• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

primitive method calls

 
Mamta Sharma
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Mike Mitchell
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike, I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic