Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing different datatype

 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void aMethod(short a)
{
S.O.P("aMethod with short called");
}
public void aMethod(int a)
{
S.O.P("aMethod with int called");
}
public void bMethod(float a)
{
S.O.P("bMethod with float called");
}

public void bMethod(double a)
{
S.O.P("bMethod with double called");
}
If I pass aMethod('a');
and bMethod(5.5);

Why does it gives result like this :
"aMethod with int called"
and
bMethod with double called
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • aMethod('a') - the compiler will automatically upcast the char to find the most specific of the overloaded methods which will work. In this case it is aMethod(int). Note that aMethod(short) won't work because the high end of char's range (65535) is out of range for a short (-32768 to 32767)
  • bMethod(5.5) - 5.5 is a double (the default type for floating point literals is double), so this one's straightforward.

  • [ September 30, 2003: Message edited by: Steve Lovelace ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic