Variable-Arity Methods

Arity is a computer science term meaning the number of arguments a method or operator takes. It was originally a joking term, taken from “polarity” meaning the number of poles something has, so by extension “arity” must mean “number of”. Well, just like your mother told you if you pulled a face it would stick like that, the term arity stuck.

image

In some languages, methods may have variable-arity which means their last or only argument is actually a list of arguments. Congratulations; with JDK 1.5, Java is now one of the languages with variable-arity! This is another hack that was put in Java because some engineers really liked C's printf() function, and wanted Java to have the same convenience.

Luckily, variable-arity (also called var-args by some) has been implemented in a very straightforward way in Java. Here's the code you write to tell the compiler “this is a variable-arity method” in Java:

public PrintWriter format(String format, Object... args)

This declaration actually comes from the API class java.io.PrintWriter. The three dots tell the compiler “args is a sequence of Objects”, so this is a variable-arity method. Inside the body of format(), the args parameter is regarded as type “array of Object”, i.e. it is exactly as though the method were declared:

public PrintWriter format(String format, Object[] args)

Where you see a difference is in calls to variable-arity methods. If the last parameter is declared with those three dots known as an ellipsis, then you can call the method with a variable number of objects as that last parameter. They will be automatically assembled into an array for you.

So you can make a call like this:

myPW.format( myFormatStr, t1, t2 );

Primitive variables will even get autoboxed for you.

myPW.format( myFormatStr, 1, 2, 3, 4 );

It doesn't buy all that much. It saves you having to create the array by hand:

Integer[] i = { 1, 2, 3, 4 };   // int literals get autoboxed
myPW.format( myFormatStr, i );

Where variable-arity shines is in formatting text. That first argument is a string that can contain ordinary text, and special format specifiers. The ordinary text is carried through literally, and the format specifiers are applied to modify the appearance of the remaining parameters before adding them to the text. The sort of thing you can do is round numbers, specify how many digits should appear, and how many characters wide they should be in all.

The format specifiers are very powerful, very involved, and really make up a special-purpose (rather ugly) programming language of their own. And they work on more than just numbers. There are format specifiers for everything you might want to print: dates, times, text messages, and so on. We'll look at format specifiers in Chapter 17. But you may now write

System.out.printf("%s is %d years old  ", name, age );

and get a result like

Ruprecht is 17 years old

You can call the same method with a different number of arguments:

System.out.printf("My favorite %d primes are %d,%d,%d ", 3, i, j, 2 );

to get the result:

My favorite 3 primes are 17,11,2

There's potential for some confusion with variable-arity methods. When you look at the call, the number of arguments no longer matches the number of parameters. You should try to restrict your use of variable-arity to the purpose intended when it was brought into Java: text string formatting and printing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.163.13