Java Reference
In-Depth Information
consider, so the compiler now considers the fourth form in phase 3. An
invocation with two arguments is compatible with a parameter list that
has a single sequence parameter, and the types of the two arguments
are assignable to the type of the sequence, so it matches. Because this
is the only match after step 1, it is the method chosen.
These rules also apply to the primitive types. An
int
, for example, can
be assigned to a
float
, and resolving an overloaded invocation will take
that into account just as it considered that a
ButteredScone
reference was
assignable to a
Scone
reference. However, implicit integer conversions to
smaller types are not appliedif a method takes a
short
argument and
you supply an
int
you will have to explicitly cast the
int
to
short
; it won't
match the
short
parameter, regardless of its value.
The method resolution process takes place at compile time based on the
declared types of the object reference and the argument values. This
process determines which form of a method should be invoked, but not
which implementation of that method. At run time the actual type of the
object on which the method is invoked is used to find an implementation
of the method that was determined at compile time.
Methods may not differ only in return type or in the list of exceptions
they throw, because there are too many ambiguities to determine which
overloaded method is wanted. If, for example, there were two
doppel-
gänger
methods that differed only in that one returned an
int
and the
other returned a
short
, both methods would make equal sense in the
following statement:
double d = doppelgänger();
A similar problem exists with exceptions, because you can catch any,
all, or none of the exceptions a method might throw in the code in which
you invoke the overloaded method. There would be no way to determine
which of two methods to use when they differed only in thrown excep-
tions.