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
doppelgä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 exceptions.
Such ambiguities are not always detectable at compile time. For example, a superclass may be modified to
add a new method that differs only in return type from a method in an extended class. If the extended class is
not recompiled, then the error will not be detected. At run time there is no problem because the exact form of
the method to be invoked was determined at compile time, so that is the method that will be looked for in the
extended class. In a similar way, if a class is modified to add a new overloaded form of a method, but the class
invoking that method is not recompiled, then the new method will never be invoked by that classthe form of
method to invoke was already determined at compile time and that form is different from that of the new
method.
Math was always my bad subject. I couldn't convince my teachers that many of my answers
were meant ironically.
Calvin Trillin
Chapter 10. Control Flow
"Would you tell me, please, which way I ought to go from here?" "That depends a good deal
on where you want to get to."
Lewis Carroll, Alice in Wonderland
A program consisting only of a list of consecutive statements is immediately useful because the statements are
executed in the order in which they're written. But the ability to control the order in which statements are
executedthat is, to test conditions and execute different statements based on the results of the testsadds
enormous value to our programming toolkit. This chapter covers almost all the control flow statements that
direct the order of execution. Exceptions and assertions are covered separately in Chapter 12.
10.1. Statements and Blocks
The two basic statements are expression statements and declaration statements, of which you've seen a
plethora. Expression statements, such as i++ or method invocations, are expressions that have a semicolon at