THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

In the BodyPrint example we invoke the println method on the object referred to by the static reference
System.out and passing a single String argument formed by concatenating a number of other strings.


Each method is declared to have a specific number of parameters, each of a specific type. The last parameter
of a method can be declared as a sequence of parameters of a given type. This is indicated by an ellipse (...)
after the type name. For example, String... is a sequence of zero or more String objects. Sequence
parameters allow you to invoke the method by passing in a variable number of arguments, some of which will
correspond to that sequence. These variable argument methods are described in the next section.


When a method is invoked the caller must provide an argument of the appropriate type for each of the
parameters declared by the method. An argument need not be exactly the same type as its corresponding
parameter because some automatic conversions are applied. For example, a byte argument can be converted
to an int parameter, and wrapper objects can be converted to their primitive values (and vice-versa) as
neededsee "Boxing Conversions" on page 198.


Methods also have a return type, either a primitive type or a reference type. If a method does not return any
value, the place where a return type would go is filled with a void.


The BodyPrint example illustrates a common situation in which you would like to examine the state of an
object. Rather than providing access to all the fields of the object, a class can define a method that returns a
string representation of the state of the object. For example, here is a method of the Body class to return a
String that describes the particular Body object it is invoked upon:


public String toString() {
String desc = idNum + " (" + name + ")";
if (orbits != null)
desc += " orbits " + orbits.toString();
return desc;
}


This method uses + and += to concatenate String objects. It first builds a string that describes the identifier
and name. If the body orbits another body, we append the string that describes that body by invoking its
toString method. This recursion builds a string of bodies orbiting other bodies until the chain ends with an
object that doesn't orbit anything. The resulting string is then returned to the caller by the return statement.


The toString method of an object is specialit is invoked to get a String when an object is used in a
string concatenation expression using the + operator. Consider these expressions:


System.out.println("Body " + sun);
System.out.println("Body " + earth);


The toString methods of sun and earth are invoked implicitly and produce the following output:


Body 0 (Sol)
Body 1 (Earth) orbits 0 (Sol)


All objects have a toString method whether their class explicitly defines one or notthis is because all
classes extend the class Object and it defines the toString method. Class extension and the Object
class are discussed in Chapter 3.

Free download pdf