File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
    It's not a secret anymore!