• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What would be the result and why ?

 
manishsharma sharma
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the result and why ?

class D {
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));

}}
Thanks
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the result


Recommend you to install the JDK on your machine.

and why


A float can comfortably accomodate a long (and of course an int) within its range.

From the JLS:
The following 19 specific conversions on primitive types are called the widening primitive conversions:
� byte to short, int, long, float, or double
� short to int, long, float, or double
� char to int, long, float, or double
int to long, float, or double
long to float or double
� float to double

[ December 12, 2006: Message edited by: Aniket Patil ]
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
float, float will be the O/P.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And where did the question come from? Please always quote the source of the mock exam. If not then we will lock the topic until you do.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m(float) is more specific that m(double) for calls using int and long.
 
manishsharma sharma
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic