Java Reference
In-Depth Information
The details for all the different format fields can be found in the Javadoc
for the java.util.Formatter class, a class that is used by printf() to do
its formatting, but one that you can also use by itself (C programmers: think
“sprintf”).
In order to implement printf() for Java, the language also had to be ex-
tended to allow for method calls with a varying number of arguments. So as of
Java 5.0, a method's argument list can be declared like this:
methodName(Type ... arglist)
This results in a method declaration which takes as its argument an array
named arglist of values of type Type . That is, it is much the same as if you
declared methodName(Type [] arglist) except that now the compiler will
let you call the method with a varying number of arguments and it will load up
the arguments into the array before calling the method. One other implication
of this is that if you have a declaration like this:
varOut(String ... slist)
then you can't, in the same class, also have one like this:
varOut(String [] alist)
because the former is just a compiler alias for the latter.
TIP
We recommend that you avoid methods with variable argument list length. You
lose the compile-time checking on the number of arguments that you supply
(since it can vary). Often the type of the arguments in the list will be Object ,
the most general type, to allow anything to be passed in. This, too, circumvents
type checking of arguments, and can lead to runtime class-cast exceptions and
other problems. Methods with variable argument list length are often a lazy
approach, but were necessary to make printf() work, and for that we
are grateful.
Search WWH ::




Custom Search