Java Reference
In-Depth Information
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
Exercise 2.10
: Add a
toString
method to
Vehicle
.
Exercise 2.11
: Add a
toString
method to
LinkedList
.
2.6.3. Methods with Variable Numbers of Arguments
The last parameter in a method (or constructor) parameter list can be
declared as a sequence of a given type. To indicate that a parameter is a
sequence you write an ellipse (
...
) after the parameter type, but before
its name. For example, in
public static void print(String... messages) {
// ...
}
the parameter
messages
is declared to be a sequence of
String
objects.
Sequence parameters allow a method to be invoked with a variable
number of arguments (including zero) that form the sequence. Such