This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing different datatype" Watch "Passing different datatype" New topic

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"
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:
    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!