This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Passing different datatype Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing different datatype" Watch "Passing different datatype" New topic
Author

Passing different datatype

thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
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

Joined: Sep 03, 2003
Posts: 125
  • 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 ]

    The Inner that is named is not the true Inner.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Passing different datatype
     
    Similar Threads
    static synchronized vs non-static synchronized
    overloading inherited method
    Method not found??
    trouble with reflection
    I can't understand these code!