Java Reference
In-Depth Information
In this example, only two versions of
f( )
are defined: one that has an
int
parameter and
one that has a
double
parameter. However, it is possible to pass
f( )
a
byte
,
short
, or
float
value. In the case of
byte
and
short
, Java automatically converts them to
int
. Thus,
f(int)
is invoked. In the case of
float
, the value is converted to
double
and
f(double)
is called.
It is important to understand, however, that the automatic conversions apply only if there
is no direct match between a parameter and an argument. For example, here is the preced-
ing program with the addition of a version of
f( )
that specifies a
byte
parameter: