THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

you invoke a method, the compiler will match each supplied argument to a named parameter. If the compiler
finds a sequence parameter then it will bundle all the remaining arguments into an array of the type specified
by the sequence parameterremember a sequence parameter must be the last declared parameter. Of course,
those arguments must be of the right type to be stored in the array. If no arguments are left to store in the array
then a zero-length array is created and passed as the argument. Knowing this, you could save the compiler the
trouble and pass an array yourself:


Body[] marsMoons = { phobos, deimos };
mars.setOrbiters(marsMoons);


But in most cases it is easier to just let the compiler do its job.


Sometimes the compiler will need some help working out what you intended a particular method invocation
to mean. For example, suppose you invoke setOrbiters with the single argument null. Does that mean
to pass null as the array (and so have no array) or does it mean to have an array of length one with a null
entry? The compiler can't be sure so it will give you a warning. To remove the ambiguity you need to cast
null to a suitable typefor a sequence parameter of type T, if you cast to T then you get an array of T of
length one with a null enTRy; if you cast to T[] then you get a null array reference. Other ambiguities
can arise in the context of method overloading, but we'll get to those later (see "Overloading Methods" on
page 69).


You've already seen an example of another varargs method in use, but may not have realized it. The printf
method that was introduced on page 23 is declared to take a String and a sequence of Objects. For each
format specifier in the format string, the method expects a corresponding object argument to be passed in.


Exercise 2.12: Considering your Vehicle and LinkedList classes, can you think of a need for any
methods that take a variable number of arguments?


2.6.4. Method Execution and Return


When a method is invoked, the flow of control passes from the calling method into the invoked method and
the statements of that method are executed in sequence according to the semantics of those statements. A
method completes execution and returns to the caller when one of three things happens: a return statement
is executed, the end of the method is reached, or an uncaught exception is thrown.


If a method returns any result, it can only return a single resulteither a primitive value or a reference to an
object. Methods that need to return more than one result can achieve this effect in several ways: return
references to objects that store the results as fields, take one or more parameters that reference objects in
which to store the results, or return an array that contains the results. Suppose, for instance, that you want to
write a method to return what a particular person can do with a given bank account. Multiple actions are
possible (deposit, withdraw, and so on), so you must return multiple permissions. You could create a
Permissions class whose objects store boolean values to say whether a particular action is allowed:


public class Permissions {
public boolean canDeposit,
canWithdraw,
canClose;
}


Here is a method that fills in the fields to return multiple values:

Free download pdf