Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloaded Methods vs. Ppcasting.

 
Arsin Delve
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not clear on what will happen if I have two overloaded methods such as
public void thisMethod (int i) {}
public void thisMethod (float f) {}
How does it choose which method to run when my argument is say a short or a byte? For that matter, if my arguement is an int, either method should accept this. How does the VM make this distinction?
It could get very complicated if I have more than one argument. Example:
Let's say I have:
void aMethod (float f, double d)
void aMethod (long l, float f)
If I make a call with an int and a float, or a short and a long, what happens?

I hope I have explained myself well. Thank you in advance for your help.
Arsin Delve.

 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arsin,
According to the JVM specs:

Where an actual argument expression corresponding to a parameter variable is not FP-strict, evaluation of that actual argument expression is permitted to use values drawn from the appropriate extended-exponent value sets. Prior to being stored in the parameter variable, the result of such an expression is mapped to the nearest value in the corresponding standard value set by method invocation conversion.

What this means is that the following is used to figure out which to method signature matches:
byte -> short -> integer -> long -> float -> double
The exception: char -> integer
For example: public void Method( int i )
can be called with:
byte b = 123;
ob.Method( b );
or
short s = 123;
ob.Method( s );
or
int i = 123;
ob.Method( i )
Regards,
Manfred.
 
Arsin Delve
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply. The only (highly unlikely) case that still remains confused in my mind is the following:

What would result from this method call?
-Arsin Delve

[This message has been edited by Arsin Delve (edited July 08, 2001).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will get error, since reference to the method is ambiguous.
Vanitha.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Often the best way to understand how these things works is to code up a program and give it a spin.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic